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>
using namespace std;
int reverse_num(int a){
int t = a, s = ;
while(t){
s = s * + t % ;
t /= ;
}
return s;
} int main(){
int t, a, b;
cin >> t;
while(t--){
cin >> a >> b;
int sum = reverse_num(a) + reverse_num(b);
while(sum % == ){
sum /= ;
}
while(sum){
cout << sum % ;
sum /= ;
}
cout << endl;
}
//system("pause");
return ;
}

方法二:为了避免反转,可以将两个数的高位开始对齐相加,设置进位标志,不好判断所得结果长度的话,可以直接用一个容器装。(这种方法可以处理大整数,将两个数看成字符串)

未能ac,来日再改

 #include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
int n, t, i, a, b;
string s1, s2, temp;
vector<int> v;
cin >> n;
while(n--){
v.clear();
cin >> s1 >> s2;
int len1 = s1.length(), len2 = s2.length();
//让s1的长度更长
if(len1 < len2){
temp = s1;
s1 = s2;
s2 = temp;
}
int flag = ; for(i = ; i < len1 && i < len2; i++){
a = s1[i] - '';
b = s2[i] - '';
t = a + b + flag;
flag = t / ;
v.push_back(t % );
}
while(i < len1){
a = s1[i] - '';
t = a + flag;
flag = t / ;
v.push_back(t % );
i++;
}
if(flag == )
v.push_back(flag);
while(){
vector<int>::iterator it = v.end() - ;
if(*it == ) v.erase(it);
else break;
}
int j = ;
while(v[j] == ){
j++;
}
int len_v = v.size();
for(; j < len_v; j++){
cout << v[j];
}
cout << endl;
}
return ;
}

zoj 2001 Adding Reversed Numbers的更多相关文章

  1. zoj2001 Adding Reversed Numbers

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2001 Adding Reversed Numbers Time ...

  2. ACM Adding Reversed Numbers(summer2017)

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

  3. poj1504 Adding Reversed Numbers

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

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

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

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

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

  6. POJ 1504 Adding Reversed Numbers

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

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

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

  8. ZOJ 1125 Floating Point Numbers

    原题链接 题目大意:给一个16位的数字,表示一个浮点数,按照规则转换成科学计数法表示. 解法:注释比较清楚了,注意浮点运算的四舍五入问题. 参考代码: #include<iostream> ...

  9. ZOJ 2405 Specialized Four-Digit Numbers

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1405 要求找出4位数所有10进制.12进制.16进制他们各位数字之和相等. # ...

随机推荐

  1. 浅谈Java中static作用--转

    static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念. 被static修饰的成员变量和成员方法独立于该类的任何 ...

  2. linux给文件或目录添加apache权限

    系统环境:ubuntu11.10/apache2/php5.3.6 在LAMP环境中,测试一个简单的php文件上传功能时,发现/var/log/apache2/error.log中出现如下php警告: ...

  3. oracle常用数据类型&约束条件(及案例)

    一.数据类型 数据类型 说明 数字 number [小数,整数] number(5,3)表示总共5个数字,小数点后3个,最大值99.999 number(5) 表示整数  最大值99999    字符 ...

  4. SpringBoot 2.x (7):拦截器

    类似以前SpringMVC的拦截器,但也有一些区别 SpringBoot的拦截器有两种方式: 第一种方式:过时的方式,适用于SpringBoot1.x的方式 package org.dreamtech ...

  5. 修改他人电脑的Windows登录密码

    在别人电脑已登录Windows的情况下: 打开控制面板 -> 管理工具 -> 计算机管理   或者  对Win图标单击右键 -> 计算机管理 -> 本地用户和组 -> 用 ...

  6. Eclipse 闪退/无法启动/一闪而过打解决办法

    解决方法 删除文件:/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi 经过实际应用真实有效.

  7. monkeyrunner 简单用例编写

    monkeyrunnerfrom com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImagedevice = Monke ...

  8. 我所理解的MVVM

    将UI中的数据适配.交互处理: controller中与UI密切相关的功能: 剥离出来,形成单独的模块: 以增加UI和Controller的灵活性.

  9. HDU 5768Lucky7(多校第四场)容斥+中国剩余定理(扩展欧几里德求逆元的)+快速乘法

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Time Limit: 2000/1000 MS (Java/Others)    M ...

  10. Linux基础命令——查看进程命令

    linux是一个 多进程   多用户的操作系统 ps(显示当前进程的状态) ps -ef  查看当前linux 进程 ps -ef | grep 'mysqld'  过滤mysql的进程 (grep  ...