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
题目大意:很容易看懂,就是将题目给的两个数倒序相加,然后在倒叙输出~
详见代码。
#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的更多相关文章
- ACM Adding Reversed Numbers(summer2017)
The Antique Comedians of Malidinesia prefer comedies to tragedies. Unfortunately, most of the ancien ...
- poj1504 Adding Reversed Numbers
Adding Reversed Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17993 Accepted: 9 ...
- zoj 2001 Adding Reversed Numbers
Adding Reversed Numbers Time Limit: 2 Seconds Memory Limit: 65536 KB The Antique Comedians of M ...
- POJ 1504 Adding Reversed Numbers (水题,高精度整数加法)
题意:给两个整数,求这两个数的反向数的和的反向数,和的末尾若为0,反向后则舍去即可.即若1200,反向数为21.题目给出的数据的末尾不会出现0,但是他们的和的末尾可能会出现0. #include &l ...
- POJ 1504 Adding Reversed Numbers
/*Sample Input 3 24 1 4358 754 305 Sample Output 34 1998 */ 值得总结的几点就是: 1.atoi函数将字符串转化为整型数字(类似于强制转换) ...
- POJ 1504,ZOJ 2001,UVA 713, Adding Reversed Numbers,错误,已找到错误
------------------------------------------------------------ 以此题警告自己: 总结, 1.在数组的使用时,一定别忘了初始化 2.在两种情况 ...
- Poj 1504 Adding Reversed Numbers(用字符串反转数字)
一.题目大意 反转两个数字并相加,所得结果崽反转.反转规则:如果数字后面有0则反转后前面不留0. 二.题解 反转操作利用new StringBuffer(s).reverse().toString() ...
- POJ 题目分类(转载)
Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...
- HDU——PKU题目分类
HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 ...
随机推荐
- C的强制转换和C++的强制转换(转)
C的强制转换: (type)<expression> 其中,type为类型描述符,如int,float等.<expression>为表达式.经强制类型转换运算符运算后,返回一个 ...
- wpf DataGrid加载行号
<DataGrid Name="tkdg" HorizontalContentAlignment="Center" AutoGenerateColumns ...
- opencv 和 parfor
一次遇到两个不熟悉的,因此在一起记一下. OpenCV的全称是:Open Source Computer Vision Library. OpenCv是一个基于(开源)发行的跨平台计算机视觉库,可以运 ...
- Android开发中如何解析Json
解析Json 有了请求,自然会有响应,那我们该如何解析服务端响应返回给我们的Json数据呢? 了解什么是Json JSON(JavaScript object notation)是一种轻量级的数据交换 ...
- Socket_SSH-3(粘包)
粘包:两次数据粘到一起了.在Windows中基本看不出来效果. 服务器端的配置: import socket,os,time server=socket.socket() server.bind((' ...
- Elasticsearch cat Apis
1._cat列入所有有效命令 GET /_cat 返回:有个猫...所以不难想象为啥是cat api =^.^= /_cat/allocation /_cat/shards /_cat/shards/ ...
- [洛谷P1341]无序字母对
题目大意:给一张无向图,找一条字典序最小的欧拉路径 题解:若图不连通或有两个以上的奇数点,则没有欧拉路径,可以$dfs$,在回溯时把这个节点加入答案 卡点:没有在回溯时加入答案,导致出现了欧拉路径没走 ...
- 【luogu2181】对角线
首先由于不会有三条对角线交于一点,所以过某一个交点有且只能有2条对角线 而这两条对角线实质上是确定了4个顶点(也可以看做是一个四边形的两条对角线交于一点,求四边形的数量). 因此我们只需要确定4个顶点 ...
- BZOJ1499:[NOI2005]瑰丽华尔兹——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1499 舞厅是一个N行M列的矩阵,矩阵中的某些方格上堆放了一些家具,其他的则是空地.钢琴可以在空地上滑 ...
- HDU5446:Unknown Treasure——题解
http://acm.hdu.edu.cn/showproblem.php?pid=5446 求C(n,m)%(p1p2…pk)的值,其中pi均为质数. 参考:https://www.cnblogs. ...