NYOJ-448 寻找最大数(贪心)

NYOJ-448

寻找最大数

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
 
   描述

请在整数 n 中删除m个数字, 使得余下的数字按原次序组成的新数最大,

比如当n=92081346718538,m=10时,则新的最大数是9888

 

输入

第一行输入一个正整数T,表示有T组测试数据
每组测试数据占一行,每行有两个数n,m(n可能是一个很大的整数,但其位数不超过100位,并且保证数据首位非0,m小于整数n的位数)
输出
每组测试数据的输出占一行,输出剩余的数字按原次序组成的最大新数
样例输入
2
92081346718538 10
1008908 5
样例输出
9888
98

思想:贪心算法,最优子结构达到最优全局解
第一次从头遍历cut个数,找到最大数,并将子结构起始搜索点定在它的下一位,同时将搜索结束点后移一位。


代码:
 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int main(void)
 5 {
 6     int T;
 7     scanf("%d", &T);
 8     while (T--)
 9     {
10         char string[101];
11         int cut;
12         scanf("%s", string);
13         scanf("%d", &cut);
14         int len = strlen(string);
15         int find_time = len - cut;
16         char cur_max;
17         int index = 0;
18         while (find_time--)
19         {
20             cur_max = string[index];
21             for (int i = index; i <= cut; i++)
22             {
23                 if (string[i] > cur_max)
24                 {
25                     cur_max = string[i];
26                     index = i;
27                 }
28             }
29             printf("%c", cur_max);
30             cut++, index++;
31         }
32         putchar('\n');
33     }
34     return 0;
35 }

 

posted @ 2016-12-22 01:32  codinRay  阅读(318)  评论(0编辑  收藏  举报