标签:
请在整数 n 中删除m个数字, 使得余下的数字按原次序组成的新数最大,
比如当n=92081346718538,m=10时,则新的最大数是9888
2 92081346718538 10 1008908 5
9888 98
/*贪心:
先算出字符数组的长度,减去要删去的位数,得到剩余的位数
第一步查找先从str[0]-str[len-m]之间找出最大值,记下temp=i的位置,并且把最大值存到数组中
第二步从temp+1的位置开始查找,在str[temp+1]-str[len-m+1]之间找出最大值,直至len-m==0为止*/
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str[101],num[101],max;
int i,j,t,m,len,k,f,mark,temp;
cin>>t;
while(t--)
{
f=0;
cin>>str>>m;
len=strlen(str);
m=len-m;
mark=0;
temp=0;
max='0';
do
{
for(i=len-m;i>=mark;i--)
{
if(str[i]>=max)
{
max=str[i];
temp=i;
}
}
num[f++]=max;
mark=temp+1;
max='0';
}while(--m);
for(i=0;i<f;i++)
cout<<num[i];
cout<<endl;
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/lwd2621/article/details/51329257