zoukankan      html  css  js  c++  java
  • 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

    Hint


    Submit Page

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<set>
    #include<string>
    #include<algorithm>
    using namespace std;
    
    int main()
    {
    	ios::sync_with_stdio(false);
    	int T,k;
    	string str;
    	cin>>T;
    	while(T--)
    	{
    		int temp=0;
    		cin>>k>>str;
    		while(k)
    		{
    			int Max=str[temp]-'0',flag=temp;
    			int pos=temp+k>str.length() ? str.length()-1 : temp+k;
    			for(int i=temp+1;i<=pos;i++)
    			{
    				if(str[i]-'0'>Max) Max=str[i]-'0',flag=i;
    			}
    			k-=flag-temp;
    			//cout<<flag<<endl;
    			
    			int ff=str[flag]-'0';
    			for(int i=flag-1;i>=temp;i--)				
    				str[i+1]=str[i];				
    			str[temp]=ff+'0';
    			temp++;
    			if(temp>=str.length()) break;
    		//	cout<<k<<endl;
    		}
    		cout<<str<<endl;
    	}
    	
    	return 0;
    }
    /**********************************************************************
    	Problem: 1270
    	User: song_hai_lei
    	Language: C++
    	Result: AC
    	Time:12 ms
    	Memory:2180 kb
    **********************************************************************/
    



  • 相关阅读:
    动手动脑
    原码反码补码
    考试感想
    第八周
    第七周
    第六周
    第五周
    第四周
    Hamburger Magi(hdu 3182)
    Doing Homework(hdu)1074
  • 原文地址:https://www.cnblogs.com/csushl/p/9386550.html
Copyright © 2011-2022 走看看