寻找最大数(三)
时间限制:
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
-
刚开始以为是直接把找到的最大值替换结果WA后来一想它是两个两个互换的
-
#include<cstdio> #include<cstdlib> #include<cstring> using namespace std; char str[100]; int main() { int i,n,l,p,q,e; char max,t; while(scanf("%s %d",str,&n)!=EOF){ l=strlen(str);p=n;e=0; while(1){ max='0'; q=e; if(p==0||e==l)break; for(i=e;i<=e+p;++i){ if(i>=l)break; if(str[i]>max){ max=str[i]; q=i; } } for(int j=q;j>e;--j){ t=str[j]; str[j]=str[j-1]; str[j-1]=t; } p=p-(q-e); e++; } printf("%s\n",str); } return 0; }
-
多组测试数据。