寻找最大数(三)
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
2
-
描述
-
给出一个整数N,每次可以移动2个相邻数位上的数字,最多移动K次,得到一个新的整数。
求这个新的整数的最大值是多少。
-
输入
-
多组测试数据。
每组测试数据占一行,每行有两个数N和K (1 ≤ N≤ 10^18; 0 ≤ K ≤ 100).
输出
- 每组测试数据的输出占一行,输出移动后得到的新的整数的最大值。 样例输入
-
1990 1 100 0 9090000078001234 6
样例输出
-
9190 100 9907000008001234
#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
int main()
{
int n,m;
string s;
while(cin>>s>>m){
n=s.length();
int h=0;
for(int i=0;i<n&&m!=0;i++){
int t=i;
char maxn='0';
for(int j=i;j<=m+i&&j<n;j++){//每次从k之前的范围找最大的,根据i值,第i次寻找放在是s[i]中,另一定要有j<n
if(s[j]>maxn){
maxn=s[j];
t=j;//最大值的下标
}
}
h=t;
while(t>i){//更新换位
int temp=s[t];
s[t]=s[t-1];
s[t-1]=temp;
t--;
}
m-=(h-i);//还剩余移动次数
}
cout<<s<<endl;
}
return 0;
}