标签:style io os ar for sp 数据 div on
ACM_赵铭浩
题目大意:……删去m位数,输出剩余的数字 使新数最大
思路:贪心思想。设位数为len,删去m位数,输出新数,就是输出新数为len-m位
根据贪心思想。从最高位开始,每次保证取出来的数字都是最优的。
比如说7位数,删去3位数。应该从第一位到第len-2位上取最大值。这样首先保证最高位
千位上的结果正确。再从刚才找到值的下一位开始到第len-1位上取最大值。保证百位上
的结果正确。再从刚才找到值的下一位开始到第len位上取最大值,保证各位上结果正确。
比如:9456973 4
因为要删去4个数,所以输出新数为3位。
从第一位9开始到第7-2=5位,找到千位上的最大值,并且尽可能靠左。找到第一位上的
第一个9,则千位为9。再从第二位4开始到第7-1位,找到百位上的最大值,找到第5位上
的第二个9。再从第6位开始到第7位,找到个位上的最大值,找到第6位上的7。
则输出结果为997.
#include<stdio.h>
#include<string.h>
char ch[110];
int main()
{
int T,num;
scanf("%d",&T);
while(T--)
{
memset(ch,0,sizeof(ch));
getchar();
scanf("%s %d",ch,&num);
int len = strlen(ch);
num = len - num;
int pos = 0;
while(num > 0)
{
int max = -1;
int j;
for(j = pos; j <= len-num; j++)
{
if(ch[j]-'0' > max && ch[j]!='a')
{
max = ch[j]-'0';
pos = j;
}
}
printf("%c",ch[pos]);
ch[pos] = 'a';
num--;
}
printf("\n");
}
return 0;
}
标签:style io os ar for sp 数据 div on
原文地址:http://blog.csdn.net/lianai911/article/details/40078353