zoj 2001 Adding Reversed Numbers
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的更多相关文章
- zoj2001 Adding Reversed Numbers
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2001 Adding Reversed Numbers Time ...
- 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 ...
- POJ 1504,ZOJ 2001,UVA 713, Adding Reversed Numbers,错误,已找到错误
------------------------------------------------------------ 以此题警告自己: 总结, 1.在数组的使用时,一定别忘了初始化 2.在两种情况 ...
- 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 Adding Reversed Numbers(用字符串反转数字)
一.题目大意 反转两个数字并相加,所得结果崽反转.反转规则:如果数字后面有0则反转后前面不留0. 二.题解 反转操作利用new StringBuffer(s).reverse().toString() ...
- ZOJ 1125 Floating Point Numbers
原题链接 题目大意:给一个16位的数字,表示一个浮点数,按照规则转换成科学计数法表示. 解法:注释比较清楚了,注意浮点运算的四舍五入问题. 参考代码: #include<iostream> ...
- ZOJ 2405 Specialized Four-Digit Numbers
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1405 要求找出4位数所有10进制.12进制.16进制他们各位数字之和相等. # ...
随机推荐
- MyBatsi-Mapper映射文件
Mapper映射文件 cache – 给定命名空间的缓存配置. cache-ref – 其他命名空间缓存配置的引用. resultMap – 是最复杂也是最强大的元素,用来描述如何从数据库结果集中来加 ...
- and or类比c中的 bool?a :b
a = "heaven" b = "hell" c = True and a or b print c d = False and a or b ...
- yum卸载遇到的问题--待解决
系统版本是 [root@master ~]# uname -a Linux master -.el6.x86_64 # SMP Sun Nov :: EST x86_64 x86_64 x86_64 ...
- KMS算法
解题:http://hihocoder.com/problemset/problem/1015 KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J.H.Morris和V.R.Pratt同时 ...
- 学习Python的day1
自己以前从来没有写博客的想法,但是学Python,里面的老师也说了,写博客可以加深自己的记忆,也能回顾内容.还能给别人参考.挺值的.2017-09-16 一. Python介绍 python的创始人为 ...
- iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...
- Android学习笔记--Intent
Intent是android四大组件之间交互的一种重要方式.Intent可以指明当前要执行的动作,也可以指明要传递的数据.Intent可以用来启动活动,启动服务,发送广播. Intent分为两种:1. ...
- EEPROM介绍
EEPROM( Electrically Erasable Programmable Read Only Memory )全称是电气可擦除可编程只读存储器,是非易失存储器,可以访问到每个字节,容量比较 ...
- 飞秋软件的OA消息接口服务器
由于单位使用了飞秋,同时也使用了OA,但OA的消息系统没有飞秋方便,所以大多数人还是在用飞秋沟通.但审批等流程又在OA上,所以做了个消息接口服务器,提取OA消息自动发送到飞秋上,大大方便了工作. 正好 ...
- UVA 11971 Polygon 多边形(连续概率)
题意: 一根长度为n的木条,随机选k个位置将其切成k+1段,问这k+1段能组成k+1条边的多边形的概率? 思路: 数学题.要求的是概率,明显与n无关. 将木条围成一个圆后再开切k+1刀,得到k+1段. ...