zoj2001 Adding Reversed Numbers
2015-10-12 14:21
337 查看
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2001
Adding Reversed NumbersTime Limit: 2 Seconds Memory Limit: 65536 KBThe 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
题目大意:很容易看懂,就是将题目给的两个数倒序相加,然后在倒叙输出~
详见代码。
第二种比较简单。
Adding Reversed NumbersTime Limit: 2 Seconds Memory Limit: 65536 KBThe 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 }
相关文章推荐
- hdu 1070 Milk(贪心)
- hdu 2545 树上战争(并查集)
- hdu 2191悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(改编01背包)
- 实现ASP.NET无刷新下载并提示下载完成的开发思路
- hdu 2602 Bone Collector (简单01背包)
- hdu 1599 find the mincost route (最小环与floyd算法)
- JavaScript与HTML结合的基本使用方法整理
- hdu 3371(prim算法)
- Java对象克隆问题
- hdu 1598 find the most comfortable road (并查集+枚举)
- hdu 1879 继续畅通工程 (并查集+最小生成树)
- hdu 1272 小希的迷宫(并查集+最小生成树+队列)
- hdu 1232 畅通工程(并查集算法)
- hdu 1213 How Many Tables(并查集算法)
- hdu 1863 畅通工程 (并查集+最小生成树)
- hdu 1230 火星A+B
- windows embedded standard 7 (wes 7正版密钥激活方法)
- Android 项目(一):网络请求封装(一)
- hdu 1102 Constructing Roads (最小生成树)
- hdu 1875 畅通工程再续