删数问题(贪心) SDUT

删数问题

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

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

Input

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

Output

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

若输出的数首位是0,则去掉0后输出。例如:024,输出24,不能输出 024 。

Sample Input

178543  4

Sample Output

13

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char n[110];///为什么没初始化但是里面全是 '\0'
int main()
{
    int i, s, lenth;
    while(~scanf("%s %d", n, &s))
    {
        lenth = strlen(n);///计算输入的整数的长度
        while(s--)
        {
            i = 0;///注意每次删数开始前都要重置 i !!!
            while(i < lenth && n[i] <= n[i + 1])
                i++;///找到递减区间的第一个元素
            while(i < lenth)
            {
                ///将该元素用其后面元素覆盖,并另其他元素前移
                n[i] = n[i + 1];
                i++;
            }
        }
        ///输出部分,如果新数的高位是0,则前面的0都不要输出
        lenth = strlen(n);
        i = 0;
        while(i < lenth && n[i] == '0')///注意此处0是符号0!
            i++;
        if(i < lenth)
        {
            while(i < lenth)
            {
                printf("%c", n[i]);
                i++;
            }
        }
        else
        {///如果去掉高位的0后没有其他数,即新数 = 0,则输出0
            printf("0");
        }
        printf("\n");
    }
    return 0;
}

 

删数问题解答:

1. 贪心算法----删数问题

https://www.cnblogs.com/cxmhy/p/4481573.html

2. 删数问题(典型的贪心算法问题)

https://blog.csdn.net/yyyyyy11123/article/details/74079776

3. 贪心,删数问题

https://www.cnblogs.com/martinue/p/5490595.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值