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
    **********************************************************************/
    



  • 相关阅读:
    MySQL数据库基准压力测试工具之MySQLSlap使用实例
    WPF水珠效果按钮组
    获取用户Ip地址通用方法常见安全隐患(HTTP_X_FORWARDED_FOR)
    leaflet的入门开发(一)
    linux 安装pip 和python3
    python模拟Get请求保存网易歌曲的url
    Phalcon 上下文编码(Contextual Escaping)
    (七十七)地理编码与反地理编码
    设计模式简单介绍
    windows下安装Jenkins
  • 原文地址:https://www.cnblogs.com/csushl/p/9386550.html
Copyright © 2011-2022 走看看