标签:style blog http io ar color os sp for
请在整数 n 中删除m个数字, 使得余下的数字按原次序组成的新数最大,
比如当n=92081346718538,m=10时,则新的最大数是9888
2 92081346718538 10 1008908 5
9888 98
思路:若求最大数,需要将m+1~len-1的数先保留,令end=m,
从0~end中找出最大的数字。记下下标t,输出最大数字,end++,
再从t+1~end中找出最大的数字,输出, 以此类推,即求得最大数!
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char s[102];
int main()
{
int i,j,m,t,k,len,test,end,ans;
scanf("%d",&test);
while(test--)
{
scanf("%s%d",s,&m);
len=strlen(s);
end=m;
t=0;
for(i=0,k=0;i<=end;)
{
ans=s[i];
for(j=i+1;j<=end;j++)
{
if(s[j]>ans)
{
ans=s[j];
t=j;
}
}
end++;
k++;
i=t+1;
printf("%c",s[t]);
t++;
if(k==len-m)//t控制输出数的位数,结束的标志。
break;
}
printf("\n");
}
return 0;
}
标签:style blog http io ar color os sp for
原文地址:http://blog.csdn.net/hdd871532887/article/details/41677897