刪數問題 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic PRoblem Description
鍵盤輸入一個高精度的正整數n(≤100位),去掉其中任意s個數字后剩下的數字按照原來的左右次序組成一個新的正整數。編程對給定的n與s,尋找一種方案,使得剩下的數字組成的新數最小。 Input
輸入有多組 每組包括原始數n,要去掉的數字數s; Output
輸出去掉s個數后最小的數 Example Input
178543 4 Example Output
13 Hint
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){ int i = 0,len; int s; char a[101]; while(~scanf("%s %d",a,&s)){ len = strlen(a); if(s>=len){ printf("0/n"); break; } while(s){//貪心一手 i = 0; len = strlen(a); while(i<len&&a[i]<=a[i+1])//只要后一個比前一個大就不往后走 i++; while(i<len){//直接把那個比前一個大的吞掉 a[i] = a[i+1]; i++; } s--; } i = 0; while(a[i]=='0')//這里是去除前導為0的情況 i++; int j = 0; while(i<len){ a[j] = a[i]; i++; j++; } if(a[0]=='/0'){//如果全部都是0的話,直接輸出0 printf("0/n"); break; } printf("%s/n",a); } return 0;}新聞熱點
疑難解答