Accept: 747 Submit: 1500
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
Now you are given one non-negative integer n in 10-base notation, it will only contain digits ('0'-'9'). You are allowed to choose 2 integers i and j, such that: i!=j, 1≤i<j≤|n|, here |n| means the length of n’s 10-base notation. Then we can swap n[i] and n[j].
For example, n=9012, we choose i=1, j=3, then we swap n[1] and n[3], then we get 1092, which is smaller than the original n.
Now you are allowed to operate at most M times, so what is the smallest number you can get after the operation(s)?
Please note that in this problem, leading zero is not allowed!
Input
The first line of the input contains an integer T (T≤100), indicating the number of test cases.
Then T cases, for any case, only 2 integers n and M (0≤n<10^1000, 0≤M≤100) in a single line.
Output
Sample Input
Sample Output
Source
“高教社杯”第三届福建省大学生程序设计竞赛
对就是一道简单到暴力的题目,可是因为前面卡在一道题上浪费了太多时间,这个没多少时间做,其实还是有时间做的,可是因为前面卡题了,后面这道居然反了低级错误,知道结束也没调试出来,多写题目啊,代码水平才能上
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
char ma[1010];
int main()
{int t,n,i,j,len,m,mj;
char b;
scanf("%d",&t);
while(t--)
{
scanf("%s%d",ma,&n);
len=strlen(ma);
for(i=0;i<n&&i<len;i++)//交换次数
{m=ma[i]-48;
mj=i;
for(j=i;j<len;j++)//找到第i位之后的最小值
{
if(i==0&&ma[j]-48<m&&ma[j]-48>0)//首位不能为0
{
m=ma[j]-48;
mj=j;
}
else if(i>0&&ma[j]-48<m)//其他位
{
m=ma[j]-48;
mj=j;
}
}
if(mj==i)n++;
else swap(ma[i],ma[mj]);
}
printf("%s\n",ma);
}
}