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. h5-24-百度地图-地址解析

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. JavaScript入门2

    5.document对象:Document对象是window对象的一个对象属性,代表浏览器窗口中装载的整个HTML文档.文档中的每个HTML元素对应着JavaScript对象. 因为document代 ...

  3. Asp.net MVC + Vue.js

    @{ Layout = null; } <!DOCTYPE html><html> <head> <meta charset="UTF-8" ...

  4. net MVC 四种基本 Filter

    四种基本 Filter 概述 MVC框架支持的Filter可以归为四类,每一类都可以对处理请求的不同时间点引入额外的逻辑处理.这四类Filter如下表:   使用内置的Authorization Fi ...

  5. python2和python3的区别(转)

    基本语法差异 核心类差异 Python3对Unicode字符的原生支持 Python2中使用 ASCII 码作为默认编码方式导致string有两种类型str和unicode,Python3只支持uni ...

  6. mysql安装及基本概念

    1.mysql下载安装 在官网下载5.6版本(越老稳定性越好,现在公司一般都用5.6),选择windows,64bit .下载完解压看bin目录下是否有mysql·exe和mysqld.exe. 解压 ...

  7. re匹配语法-match、search和findall

    1.re.match() 匹配第一个值 列表里的值可以有多个范围,有一个符合就可以. match只匹配第一个值,所以列表里的范围是第一个值得取值范围.如果第一个值被设定好且存在,那么列表的取值范围变为 ...

  8. postgres的强制类型转换与时间函数

    一.类型转换postgres的类型转换:通常::用来做类型转换,timestamp到date用的比较多select  now()::dateselect  now()::varchar 示例1:日期的 ...

  9. 回顾Spring MVC_01_概述_入门案例

    SpringMVC: SpringMVC是Spring为展现层提供的基于MVC设计的优秀的Web框架,是目前最主流的MVC框架之一 SpringMVC通过注解,让POJO成为处理请求的控制器,而无须实 ...

  10. 学习Python的一些Tips

    0. Python安装 官网提供多种方式,一般Windows下直接安装exe即可:Linux下基本上自带python:另外也提供源码,也可自行编译: 若安装后无法使用,则检查一下环境变量是否设置正确. ...