SDUT2072 -> 删数问题(贪心)

删数问题

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

 键盘输入一个高精度的正整数n(≤100位),去掉其中任意s个数字后剩下的数字按照原来的左右次序组成一个新的正整数。编程对给定的n与s,寻找一种方案,使得剩下的数字组成的新数最小。

Input

  输入有多组 每组包括原始数n,要去掉的数字数s;

Output

 输出去掉s个数后最小的数

Sample Input

178543  4

Sample Output

13

Hint

  关键是想到要删除的数是开始递减区间的第一个数,还要在意一下前导是0的情况。

Source


代码实现:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char str[110];
int main()
{
    int s, i, j, len;
    while(~scanf("%s %d", str, &s))
    {
        for(i = 1; i <= s; i ++)
        {
            len = strlen(str);
            j = 0;
            while(j < len && str[j] <= str[j +1]) /*找到递减的位置*/
            {
                j ++;
            }
            while(j < len) /*向前移动字符,实现删数功能*/
            {
                str[j] = str[j + 1];
                j ++;
            }
        }
        len = strlen(str);
        j = 0;
        while (j < len && str[j] == '0') j ++; //前导的0去掉
        if(j < len)
        {
            while(j < len)
            {
                printf("%c", str[j]);
                j ++;
            }
        }
        else
        {
            printf("0");
        }
        printf("\n");
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/Mercury_Lc/article/details/80029657
今日推荐