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 }
时间: 2024-10-08 09:29:43

zoj2001 Adding Reversed Numbers的相关文章

UVA713 UVALive5539 POJ1504 ZOJ2001 Adding Reversed Numbers

Regionals 1998 >> Europe - Central 问题链接:UVA713 UVALive5539 POJ1504 ZOJ2001 Adding Reversed Numbers.入门练习题,用C语言编写. 题意简述:输入两个整数,都反转(逆序)后再求和,输出相加后反转(逆序)的结果. 问题分析:整数可能很大,大到200位:相加结果反转后左边的0需要去掉. 程序中封装了两个函数来实现,逻辑更加简单清晰. AC的C语言程序如下: /* UVA713 UVALive5539 PO

zoj 2001 Adding Reversed Numbers

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

SPOJ Python Day1: Adding Reversed Numbers

水题就要水的滴水不漏=-=,第一个疗程博主决定按照SPOJ提交人数的顺序开始刷,主要任务在于熟悉Python. 42. Adding Reversed Numbers 题目非常简单 Sample input: 3 24 1 4358 754 305 794 第一行是下面输入的行数,主要说的事儿是把第一个数反过来,第二个数反过来,然后加一起,然后再把他们的和反过来.比如说24 1 这行吧: Step 1:24 –> 42, 1 –> 1 Step 2:42+1 = 43 Step 3:43 –&

Adding Reversed Numbers(summer2017)

1 /* 2 author:WTZPT 3 Time:2017.7.17 4 Title:Adding Reversed Numbers 5 */ 6 #include<stdio.h> 7 #include<math.h> 8 #include<iostream> 9 using namespace std; 10 int length(int num){ //测试数据长度 11 int i = 0; 12 while(num){ 13 num /= 10; 14 i

POJ 1504,ZOJ 2001,UVA 713, Adding Reversed Numbers,错误,已找到错误

------------------------------------------------------------ 以此题警告自己: 总结, 1.在数组的使用时,一定别忘了初始化 2.在两种情况复制代码时,一定要小心,注意修改变量名,一不留神就会带来不可估量的后果,一定要仔细挨着一个一个变量的修改,别跳着看着哪个变量就改哪一个变量! (这个题目中,就是复制了一下,代码,ca,我找了一下午的错....还好终于找到了,一个字母的错,) -----------------------------

B - Adding Reversed Numbers

B - Adding Reversed Numbers Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description The Antique Comedians of Malidinesia prefer comedies to tragedies. Unfortunately, most of the ancient plays are tragedies

POJ 1504 Adding Reversed Numbers

Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16053   Accepted: 8786 Description The Antique Comedians of Malidinesia prefer comedies to tragedies. Unfortunately, most of the ancient plays are tragedies. Therefore the dramatic advisor

SPOJ problem 42: Adding Reversed Numbers

这是一道水题,就是让你把给出的两个数倒置后相加再倒回来,模拟一下就行. #include<cstdio> int n,m,x,y,t; int main(){ for(scanf("%d",&t);t;t--){ scanf("%d%d",&n,&m); x=0;y=0; for (;n;n/=10)x=x*10+n%10; for (;m;m/=10)y=y*10+m%10; x+=y;y=0; for (;x;x/=10)y=

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY