|
Title |
我删我删,删删删 |
|
|
Problem ID |
1326 |
|
|
Time Limit |
1 |
Sec |
|
Memory Limit |
64 |
MB |
|
Description: |
||
|
有一个大整数.不超过1000位.假设有N位.我想删掉其中的任意S个数字.使得删除S位后,剩下位组成的数是最小的. |
||
|
Input: |
||
|
有多组数据数据,每组数据为两行.第一行是一个大整数.第二行是个整数S,其中S小于大整数的位数. 输入以EOF结束。 |
||
|
Output: |
||
|
对于每组输入数据,请输出其删除后的最小数. |
||
|
Sample Input: |
||
|
178543 4 100002 1 |
||
|
Sample Output: |
||
|
13 2 |
||
|
code: |
||
1 #include<stdio.h>
2 #include<string.h>
3 void delet(char yc[], char y)
4 {
5 char temp[1000];
6 int i, c1;
7 c1 = strlen(yc);
8
9 for (i = 0; i < c1; i++)
10 {
11 if (yc[i]==y)
12 {
13 strcpy(temp, yc);
14 strcpy(&yc[i], &temp[i+1]);
15 return ;
16 }
17
18 }
19 }
20 int main()
21 {
22 char a[1008];//用来存放输入数据的
23 while (scanf("%s", a) != EOF)//输入大整数
24 {
25 int i = 0,n = 0, len = 0; //i用作步长,删除n位,len是数据的长度
26 char c[1008] = { 0 };//用来消除前面没用的0
27 scanf("%d", &n);
28 /*00000000000000000000如果大整数是负数000000000000000000000000000000000*/
29 if (a[0] == '-')
30 {
31 delet(a, '-');// 变成正数
32
33 while (n--)//删除n个数
34 {
35 len = strlen(a);//计算此时大整数的位数
36 char b= a[len-1];//另b为大整数的最低位
37
38 for (i = 0; i < len - 1; i++)
39 if (a[i]< a[i + 1]){ b = a[i]; break; }
40 delet(a, b);//如果前一位小于后一位就删除前一位,保留大的
41
42 for (i = 0; i < len - 0; i++)
43 if (a[i] != '0')break;//删除前面多余的0
44
45 strcpy(c, &a[i]);
46 strcpy(a, c);
47 }
48
49 putchar('-'); puts(a);
50
51 }
52 /*0000000000000000000000000000大整数是正数0000000000000000000000000000*/
53 else
54 { while (n--)
55 {
56 len = strlen(a);
57 char b= a[len - 1];
58 for (i = 0; i < len - 1; i++)
59 if (a[i]> a[i + 1]){b = a[i]; break; }
60 delet(a, b);
61 }
62
63 for (i = 0; i < len - 1; i++)
64 if (a[i] != '0')break;
65 strcpy(c, &a[i]);
66 puts(c);
67 }
68 }
来源:https://www.cnblogs.com/anquan1501/p/5072474.html