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

    Hint

    暴力
    #include<stdio.h>
    #include<string>
    #include<string.h>
    #include<algorithm>
    #include<iostream>
    typedef long long ll;
    using namespace std;
    int T, s, len;
    char ch[1010];
    int main()
    {	
    	cin >> T;
    	while (T--)
    	{
    		cin >> s >> ch;
    		len = strlen(ch);
    		for (int i = 0; i < len; i++)
    		{
    			if (s <= 0)break;
    			char max = '0';
    			int key;
    			for (int j = i + 1; j < len && j <= i + s; j++)//找到能移动的最大位数
    			{
    				if (max < ch[j])
    				{
    					max = ch[j];
    					key = j;
    				}
    			}
    			if (max > ch[i])
    			{
    				for (int j = key; j > i; j--)
    					ch[j] = ch[j - 1];
    				ch[i] = max, s =s-( key - i);
    			}
    		}
    		cout << ch << endl;
    	}
    	return 0;
    }
    /**********************************************************************
    	Problem: 1270
    	User: leo6033
    	Language: C++
    	Result: AC
    	Time:12 ms
    	Memory:2024 kb
    **********************************************************************/
    

  • 相关阅读:
    spark学习
    推荐系统-摘录
    matplotlib安装问题解决
    秒杀系统分析
    大数据常用启动命令
    推荐系统简介
    java面试-JVM内存结构
    推荐算法-基于内容的推荐
    推荐算法-基于模型的协同过滤
    监督学习之模型评估与选择
  • 原文地址:https://www.cnblogs.com/csu-lmw/p/9124443.html
Copyright © 2011-2022 走看看