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



  • 相关阅读:
    Java:前程似锦的 NIO 2.0
    优秀的程序员都热爱写作
    Java -- JDBC 学习--获取数据库链接
    前端学习 -- Html&Css -- 条件Hack 和属性Hack
    前端学习 -- Html&Css -- ie6 png 背景问题
    前端学习 -- Html&Css -- 框架集
    ECMAScript 6 -- 字符串的扩展
    ECMAScript 6 -- 数组的解构赋值
    前端学习 -- Html&Css -- 表单
    前端学习 -- Html&Css -- 表格
  • 原文地址:https://www.cnblogs.com/csushl/p/9386550.html
Copyright © 2011-2022 走看看