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进制他们各位数字之和相等. # ...
随机推荐
- Hibernate通过实体对象对应数据库表信息
Hibernate通过实体对象对应数据库表信息,包括:数据库表名称.主键列名.非主键列名等. 获取对象映射缓存管理类: AbstractEntityPersister aep = (AbstractE ...
- hbuilder 中文乱码
这是因为HBuilder默认文件编码是UTF-8,你可以在工具-选项-常规-工作空间选项中设置默认字符编码
- org.codehaus.jettison.json.JSONObject使用方法
public static void main(String[] args) { System.out.println("测试开始"); File file = new File( ...
- solr管理界面下统计多个时间段的数据 facet.query
在Raw Query Parameters参数里面输入时间段即可 如下图所示: facet.query=publishTime:[2017-06-05T00:00:00Z TO 2017-06-07T ...
- (转 )Unity对Lua的编辑器拓展
转 http://blog.csdn.net/ZhangDi2017/article/details/61203505 当前版本的Unity(截至Unity5.5.x)中TextAsset类不支持后缀 ...
- Spring Boot学到的内容
Hello World:了解程序入口(创建启动类) Web程序:写Controller类(@RestController),写Controller方法(@GetMapping),maven依赖spri ...
- Android里的 ART、JIT、AOT、Dalvik之间有什么关系?
ART.JIT.AOT.Dalvik之间有什么关系? JIT与Dalvik JIT是"Just In Time Compiler"的缩写,就是"即时编译技术", ...
- 【数据分析 R语言实战】学习笔记 第七章 假设检验及R实现
假设检验及R实现 7.1假设检验概述 对总体参数的具体数值所作的陈述,称为假设;再利用样本信息判断假设足否成立,这整个过程称为假设检验. 7.1.1理论依据 假设检验之所以可行,其理沦背景是小概率理论 ...
- laravel模型关联
hasOne 一对一 用户名-手机号hasMany 一对多 文章-评论belongTo 一对多反向 评论-文章belongsToMany 多对多 用户-角色hasManyThrough 远程 ...
- Redis杂谈
这是2015年初应邀在南华智闻作技术交流时所作的Redis方面的一个presentation. 因为原件是Keynote格式,已经转成PDF,点击下面链接打开或者下载PDF: Redis 杂谈