zoukankan      html  css  js  c++  java
  • CSU 1270【Swap Digits】

    Description

    Now we have a number, you can swap any two adjacent digits of it, but you can not swap more than K times. Then, what is the largest probable number that we can get after your swapping?

    Input

    There is an integer T (1 <= T <= 200) in the first line, means there are T test cases in total.

    For each test case, there is an integer K (0 <= K < 106) in the first line, which has the same meaning as above. And the number is in the next line. It has at most 1000 digits, and will not start with 0.

    There are at most 10 test cases that satisfy the number of digits is larger than 100.

    Output

    For each test case, you should print the largest probable number that we can get after your swapping.

    Sample Input

    3
    2
    1234
    4
    1234
    1
    4321

    Sample Output

    3124
    4213
    4321

    • 分析:每次从能够最多移动的位数里面找到最大值,判断是否能够当前元素交换。数组循环右移index-i位,index位最大值所在下标,i为当前元素。
    • 代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <vector>
    #include <set>
    #include <map>
    using namespace std;
    int main()
    {
        int T,k,t;
        char s[1005];
        cin>>T;
        while(T--){
            int index=-1,maxx='0';
            cin>>k>>s;
            int len=strlen(s);
            for(int i=0;i<len;i++){
                maxx='0';
                index=-1;
                for(int j=i+1;j<len&&j<=i+k;j++){
                    if(s[j]>maxx){
                        maxx=s[j];
                        index=j;
                    }
                }
                if(maxx>s[i]){
                    for(int j=index;j>=i;j--){
                        s[j]=s[j-1];
                    }
                    s[i]=maxx;
                    k-=(index-i);
                }
    
            }
            puts(s);
        }
        return 0;
    }
    

  • 相关阅读:
    java源码ReentrantLock源码分析1
    java源码Semaphore源码分析
    java源码HashMap源码分析
    java源码LinkedHashMap类设计
    java源码HashMap类设计
    java源码ConcurrentHashMap分析1
    java源码CountDownLatch源码分析
    转linux误删文件恢复 简单
    「翻译」Redis协议 简单
    刘昕明:送给和我一样曾经浮躁过的PHP程序员 简单
  • 原文地址:https://www.cnblogs.com/kzbin/p/9205212.html
Copyright © 2011-2022 走看看