寻找最大数(三)

描述

给出一个整数N,每次可以移动2个相邻数位上的数字,最多移动K次,得到一个新的整数。

求这个新的整数的最大值是多少。

输入
多组测试数据。
每组测试数据占一行,每行有两个数N和K (1 ≤ N≤ 10^18; 0 ≤ K ≤ 100).
输出
每组测试数据的输出占一行,输出移动后得到的新的整数的最大值。
样例输入
1990 1
100 0
9090000078001234 6
样例输出
9190
100
9907000008001234

分析:

给定一个大数n,给定一个数k代表移动的次数,要求移动k次,(每次只能移动相邻的两个数),使得移动之后的数n达到最大




#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <queue>
using namespace std;
const int maxn = 105;

int main()
{
    string n;
    int k;
    while(cin >> n >> k)
    {
        int t;
        int len = n.length();
        for(int i = 0;i < len&&k != 0;i++)
        {
            t = i;
            for(int j = i+1;j <= i+k&&j < len;j++)
            {
                if(n[t] < n[j])
                    t = j;
            }
            for(int j = t;j > i;j--)
            {
                swap(n[j],n[j-1]);
            }
            k-=(t-i);
        }
        cout << n << endl;
    }
    return 0;
}









猜你喜欢

转载自blog.csdn.net/lidafoye/article/details/77971413