POJ 1504 Adding Reversed Numbers (水)

先上题:POJ 1504

Describe:
The Antique Comedians of Malidinesia prefer comedies to tragedies. Unfortunately, most of the ancient plays are tragedies. Therefore the dramatic advisor of ACM has decided to transfigure some tragedies into comedies. Obviously, this work is very hard because the basic sense of the play must be kept intact, although all the things change to their opposites. For example the numbers: if any number appears in the tragedy, it must be converted to its reversed form before being accepted into the comedy play.

Reversed number is a number written in arabic numerals but the order of digits is reversed. The first digit becomes last and vice versa. For example, if the main hero had 1245 strawberries in the tragedy, he has 5421 of them now. Note that all the leading zeros are omitted. That means if the number ends with a zero, the zero is lost by reversing (e.g. 1200 gives 21). Also note that the reversed number never has any trailing zeros.

ACM needs to calculate with reversed numbers. Your task is to add two reversed numbers and output their reversed sum. Of course, the result is not unique because any particular number is a reversed form of several numbers (e.g. 21 could be 12, 120 or 1200 before reversing). Thus we must assume that no zeros were lost by reversing (e.g. assume that the original number was 12).
Input:
The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly one line with two positive integers separated by space. These are the reversed numbers you are to add.
Output:
For each case, print exactly one line containing only one integer - the reversed sum of two reversed numbers. Omit any leading zeros in the output.
Sample Input:
3
24 1
4358 754
305 794
Sample Output:
34
1998
1

题目大意:顺便练习六级翻译

(省略废话),给两个数,先反转,相加求和,再将和反转输出,注意前导0,反转前末尾有0,反转后则0省略,然后输出结果。

解题思路:

整体思路比较简单,数没有范围,还牵扯到反转,考虑用高精,一些细节会在代码注释给出,只涉及到了高精加法,算是高精入门水题了。

AC代码:

 

 1 #include 
 2 #include 
 3 #include 
 4 /*
 5 着重强调这个  ,如果没有这个头文件,并且OJ上提交代码用的是C++,会CE,就是cin不能
 6 输入string类,据说是因为头文件不兼容(不懂),但如果用的是G++,不加这个头文件也没事。
 7 */
 8 #include <string>
 9 using namespace std;
10 string s1;  // 两个string类,输入的两个数
11 string s2;
12 int a[100],b[100],c[100];  //存两个高精和结果的数组,为什么是100,我随便开的
13 int k,n1,n2,n;  // k是样例数,其他的都是高精数长度
14 int main()
15 {
16     cin >> k;
17     while(k--)
18     {
19         // 重点!每一次都要清零,不然会出错
20         memset(a,0,sizeof(a));
21         memset(b,0,sizeof(b));
22         memset(c,0,sizeof(c));
23         // 常规输入,不解释了
24         cin >> s1;
25         n1 = s1.size();
26         for(int i = 0; i < n1; i++)
27             a[i] = s1[i]-'0';
28         // 同上
29         cin >> s2;
30         n2 = s2.size();
31         for(int i = 0; i < n2; i++)
32             b[i] = s2[i]-'0';
33         // 高精加法,easy
34         n = n1>n2?n1:n2;
35         for(int i = 0; i < n; i++)
36         {
37             c[i] += a[i]+b[i];
38             if(c[i] >= 10)
39             {
40                 c[i] = c[i]-10;
41                 c[i+1]++;
42             }
43             if(i == n-1 && c[i+1] != 0) {n++; break;} // 这里注意,要考虑相加结果位数会不会多一
44         }
45         int f = 0; // 标志变量,用来除去前导零
46         // 除前导零办法应该挺多的,按着自己思路写就可以了
47         for(int i = 0; i < n; i++)
48         {
49             if(c[i] == 0 && f == 0) continue;
50             if(c[i] == 0 && f != 0) cout << c[i];
51             if(c[i] != 0) {f = 1; cout << c[i];}
52         }
53         cout << endl; // 不要忘了换行
54     }
55     return 0;
56 }

 

小结:虽然简单,但是细节比较多,还是需要多练习,多熟悉。

 

你可能感兴趣的:(POJ 1504 Adding Reversed Numbers (水))