删数问题
Time Limit: 1000 ms Memory Limit: 65536 KiB
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. 贪心,删数问题