Codeforces Round #479 (Div. 3) A. Wrong Subtraction

题目网址:http://codeforces.com/contest/977/problem/A

题解:给你一个数n,进行k次变换,从末尾开始-1,512变成511,511变成510,510会把0消掉。(看Note应该都能看懂的吧~)

方法:水题。。。把数字用字符串读入,遇到末尾为0的情况就把字符串长度-1,不然就-1。然后len<=0的情况就输出0(不知道为什么不用<0就可以过了,可能不会出现这样的情况?),反之按长度一个个输出即可~

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<string>
 6 #include<iostream>
 7 #include<map>
 8 #include<vector>
 9 #include<set>
10 #include<queue>
11 using namespace std;
12 int main()
13 {
14     char n[10];
15     int  k;
16     cin >> n >> k;
17     int len = strlen(n);
18     while (k--)
19     {
20         if (n[len - 1] == '0')
21         {
22             len--;
23         }
24         else
25         {
26             n[len - 1] -= 1;
27         }
28     }
29     if (len == 0)
30         printf("0\n");
31     else
32     {
33         for (int i = 0; i < len; i++)
34         {
35             printf("%c", n[i]);
36         }
37         printf("\n");
38     }
39     
40     return 0;
41 }

猜你喜欢

转载自www.cnblogs.com/Tangent-1231/p/9005389.html