首页 > 代码库 > zoj2001 Adding Reversed Numbers

zoj2001 Adding Reversed Numbers

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2001

Adding Reversed Numbers

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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

 

题目大意:很容易看懂,就是将题目给的两个数倒序相加,然后在倒叙输出~

详见代码。

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4  5 using namespace std; 6  7 int main () 8 { 9     int n;10     scanf("%d",&n);11     while (n--)12     {13         char a[100],b[100];14         scanf("%s%s",a,b);15         int len1=strlen(a);16         int len2=strlen(b);17         int sum1=0;18         for (int i=len1-1;i>=0;i--)19             sum1=sum1*10+a[i]-0;20         int sum2=0;21         for (int i=len2-1;i>=0;i--)22             sum2=sum2*10+b[i]-0;23         int sum=sum1+sum2;24         int s=0;25         while (sum)26         {27             s=sum%10+s*10;28             sum/=10;29         }30         printf ("%d\n",s);31     }32     return 0;33 }

第二种比较简单。

 1 #include <iostream> 2 #include <cstdio> 3  4 using namespace std; 5  6 int fun(int k) 7 { 8     int sum=0; 9     while (k)10     {11         sum=k%10+sum*10;12         k/=10;13     }14     return sum;15 }16 17 int main ()18 {19     int n,a,b;20     scanf("%d",&n);21     while (n--)22     {23         scanf("%d%d",&a,&b);24         printf ("%d\n",fun(fun(a)+fun(b)));25     }26     return 0;27 }

 

zoj2001 Adding Reversed Numbers