题目链接: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

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

详见代码。

 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; int main ()
{
int n;
scanf("%d",&n);
while (n--)
{
char a[],b[];
scanf("%s%s",a,b);
int len1=strlen(a);
int len2=strlen(b);
int sum1=;
for (int i=len1-;i>=;i--)
sum1=sum1*+a[i]-'';
int sum2=;
for (int i=len2-;i>=;i--)
sum2=sum2*+b[i]-'';
int sum=sum1+sum2;
int s=;
while (sum)
{
s=sum%+s*;
sum/=;
}
printf ("%d\n",s);
}
return ;
}

第二种比较简单。

 #include <iostream>
#include <cstdio> using namespace std; int fun(int k)
{
int sum=;
while (k)
{
sum=k%+sum*;
k/=;
}
return sum;
} int main ()
{
int n,a,b;
scanf("%d",&n);
while (n--)
{
scanf("%d%d",&a,&b);
printf ("%d\n",fun(fun(a)+fun(b)));
}
return ;
}

zoj2001 Adding Reversed Numbers的更多相关文章

  1. ACM Adding Reversed Numbers(summer2017)

    The Antique Comedians of Malidinesia prefer comedies to tragedies. Unfortunately, most of the ancien ...

  2. poj1504 Adding Reversed Numbers

    Adding Reversed Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17993 Accepted: 9 ...

  3. zoj 2001 Adding Reversed Numbers

    Adding Reversed Numbers Time Limit: 2 Seconds      Memory Limit: 65536 KB The Antique Comedians of M ...

  4. POJ 1504 Adding Reversed Numbers (水题,高精度整数加法)

    题意:给两个整数,求这两个数的反向数的和的反向数,和的末尾若为0,反向后则舍去即可.即若1200,反向数为21.题目给出的数据的末尾不会出现0,但是他们的和的末尾可能会出现0. #include &l ...

  5. POJ 1504 Adding Reversed Numbers

    /*Sample Input 3 24 1 4358 754 305 Sample Output 34 1998 */ 值得总结的几点就是: 1.atoi函数将字符串转化为整型数字(类似于强制转换) ...

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

    ------------------------------------------------------------ 以此题警告自己: 总结, 1.在数组的使用时,一定别忘了初始化 2.在两种情况 ...

  7. Poj 1504 Adding Reversed Numbers(用字符串反转数字)

    一.题目大意 反转两个数字并相加,所得结果崽反转.反转规则:如果数字后面有0则反转后前面不留0. 二.题解 反转操作利用new StringBuffer(s).reverse().toString() ...

  8. POJ 题目分类(转载)

    Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...

  9. HDU——PKU题目分类

    HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 ...

随机推荐

  1. Jmeter系列-webdriver代码范例

    范例 WDS.sampleResult.sampleStart() try{ //打开博客首页 WDS.browser.get('http://xqtesting.blog.51cto.com') / ...

  2. Jdk1.7 与 jdk1.8的区别,最新的特征有哪些(美团,360,京东面试题目)

    在jdk7的新特性方面主要有下面几方面的增强: 1.1二进制变量的表示,支持将整数类型用二进制来表示,用0b开头. 所有整数int.short.long.byte都可以用二进制表示: byte aBy ...

  3. JAVA IDE IntelliJ IDEA使用简介(二)—之基本操作

    一.在编辑器中打开文件  1.可以使用下面的几种方式打开project内的文件进行编辑  (·)在project窗口中双击需要编辑的文件.  (·)在project窗口选择需要编辑的文件,按F4  ( ...

  4. [Leetcode] 2.Add Two Numbers(List To Long,模拟)

    本题题意是指将两个数倒序存储在链表中,再将两数之和同样存储在链表中输出. 我最开始的思路是将每一位相加,再考虑是否进位,但这时就需要考虑一些情况,比较麻烦. 于是我决定采取另一种在网上新学到的方法:这 ...

  5. SpringBoot2.0(三) 文件上传

    SpringBoot中发起文件上传示例: /** * 文件上传 * @param multipartFile * @param path * @return */ @RequestMapping(va ...

  6. poj1456——Supermarket

    Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14656   Accepted: 6656 Desc ...

  7. hadoop 使用Avro求最大值

    在上例中:hadoop MapReduce辅助排序解析,为了求每年的最大数据使用了mapreduce辅助排序的方法. 本例中介绍利用Avro这个序列化框架的mapreduce功能来实现求取最大值.Av ...

  8. 【Codeforces Round #405 ( Div 2)】题解

    Bear and Big Brother 签到题,直接模拟就可以了. Bear and Friendship Condition 满足只能是每个朋友圈中每个人和其他人都是朋友,这样的边数的确定的. 然 ...

  9. Docker-端口映射

    Docker-端口映射 Docker端口映射 docker容器在启动的时候,如果不指定端口映射参数,在容器外部是无法通过网络来访问容器内的网络应用和服务的. 亦可使用Dockerfile文件中的EXP ...

  10. AT1984 Wide Swap

    AT1984 Wide Swap 题意翻译 给出一个元素集合为\(\{1,2,\dots,N\}(1\leq N\leq 500,000)\)的排列\(P\),当有\(i,j(1\leq i<j ...