题目链接: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. python爬虫-使用xpath方法

    #coding=utf-8 import re from lxml import etree import requests response = requests.get("http:// ...

  2. centos7 下pycharm无法输入中文问题解决方案

    作者使用的pycharm是2017.2 在pycharm.sh脚本的如下行(大约在201行): # -------------------------------------------------- ...

  3. JXM 监控tomcat 7(含代码

    1.在tomcat的server.xml中加入: <Listener className="org.apache.catalina.mbeans.JmxRemoteLifecycleL ...

  4. windows 2008 iis7 上传大文件限制的真正解决办法

    以前做了一个网站 ,当时本机测试时上传文件大小没有问题,上G也应该可以,可是放在服务器后只能上传小于30M以下文件,当时基本需要也基本在30M以下,就没有管,后在网上发现原来是window2008本身 ...

  5. Lucene笔记二

    lucene 的排序 package cn.itcast.lucene; import java.io.IOException; import org.apache.lucene.document.D ...

  6. Codeforces Round #522 Div. 1 没打记

    开场被A劝退,写了得有50min于是不敢交了.unrated了喜闻乐见. A:瞎猜都能猜到如果要走到那条直线上,进入直线的点横坐标或纵坐标与起点相同,离开直线的点横坐标或纵坐标与终点相同,证明脑补一下 ...

  7. 【刷题】洛谷 P2709 小B的询问

    题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重 ...

  8. bzoj 1037: [ZJOI2008]生日聚会Party (dp)

    dp,但是要顺推容易点 const mm=; var f:..,..,..,..]of longint; n,m,kk,now,sum,i,j,k1,k2:longint; function max( ...

  9. POJ2187:Beauty Contest——题解

    http://poj.org/problem?id=2187 题目大意:给n个点,求点对最大距离的平方. ———————————————————— 很容易证明最大距离的点对在最大凸包上. 那么就是旋转 ...

  10. BZOJ3938 & UOJ88:[集训队互测2015]Robot——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3938 http://uoj.ac/problem/88 小q有n只机器人,一开始他把机器人放在了一 ...