1529:寻找最大数
Description
给出一个整数n每次可以移动相邻数位上的数字,最多移动k次,得到一个新的整数,求这个新的整数的最大值是多少。
Input
多组测试数据。
每组测试数据占一行,每行有两个数N和K (1 ≤ N≤ 10^100; 0 ≤ K ≤ 100).
Output
每组测试数据的输出占一行,输出移动后得到的新的整数的最大值。
Sample Input
1990 1
100 0
9090000078001234 6
Sample Output
9190
100
9907000008001234
#include<iostream>
#include<string>
using namespace std;
int main()
{
string N;
int K;
while(cin>>N>>K)
{
int i,length1,j,m;
length1=N.length();
if(K==0)
{
cout<<N<<endl;
continue;
}
for(i=0;i<length1-1;i++)
{
char max=N[i];
m=i;
for(j=i+1;j<=i+K;j++)
{
if(j==length1)
break;
if(max<N[j])
{
max=N[j];
m=j;
}
}
for(int a=m;a>i&&K;a--)
{
char t=N[a];
N[a]=N[a-1];
N[a-1]=t;
K--;
}
if(K==0)
break;
}
cout<<N<<endl;
}
}