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进制他们各位数字之和相等. # ...
随机推荐
- 第03课 在VMwave 14.0 上配置企业级CentOS 6.6操作系统
第一部分:配置虚拟硬件 1.1 启动VMware,选择文件-->新建虚拟机(Ctrl + N),创建一个虚拟机. (VMware的安装过程较为简单,可自行百度.) 1.2 此时,出现新建虚拟机向 ...
- Android-apk文件反编译
一:工具介绍及下载 1:apktool 作用:资源文件获取,可以提取出图片文件和布局文件进行使用查看 2:dex2jar 作用:将apk反编译成java源码(classes ...
- Mysql常用命令行大全(转)
第一招.mysql服务的启动和停止 net stop mysql net start mysql 第二招.登陆mysql 语法如下: mysql -u用户名 -p用户密码 键入命令mysql -uro ...
- python_11(网络编程)
第1章 ucp协议 1.1 特性 1.2 缺陷 1.3 UDP协议实时通信 第2章 socket的更多方法 2.1 面向锁的套接字方法 2.1.1 blocking设置非阻塞 2.1.2 Blocki ...
- CentOS 7不重启刷新磁盘列表
[root@master-09:29:09 33~]#ls /sys/class/scsi_host/host0 host1 host2[root@master-09:29:55 34~]#echo ...
- Linux遗忘命令
1.查找文件的安装目录,拿nginx来说 find /|grep nginx.conf 2. a.查询某个端口是否被占用,如8080端口 netstat –apn | grep 8080 b.查看 ...
- C#中分部类和分部方法的应用
本篇文章介绍了,C#中分部类和分部方法的应用.需要的朋友参考下 分部类(Partial Class)在C#2.0引入,分部方法(Partial Method)在C#3.0引入,这两个语法特性都 ...
- Haproxy+Rabbitmq中的问题
问题一.Rabbitmq集群搭建完成 某个集群节宕机后 无法添加失败 解决办法:停掉所有Rabbitmq服务 并删除集群文件C\Users\Administrator\AppData\Roaming\ ...
- ES-Apache Lucene
前言 在介绍Lucene之前,我们来了解相关的历史. 有必要了解的Apache Apache软件基金会(也就是Apache Software Foundation,简称为ASF)是专门为运作一个开源软 ...
- iOS9 开发新特性 Spotlight使用
1.Spotloight是什么? Spotlight在iOS9上做了一些新的改进, 也就是开放了一些新的API, 通过Core Spotlight Framework你可以在你的app中集成Spotl ...