编程练习2
来源:互联网 发布:php webservice 编辑:程序博客网 时间:2024/06/05 01:18
Description
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).
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
324 14358 754305 794
Sample Output
3419981
#include<iostream>#include<cstdio>#include<cmath>#include<string>#include<cstring>using namespace std;int main(){ string s1,s2; char s3[4]; long l1,l2,l,p,n,i,j,m[4],a,b,k=0,s=0,z; cin>>n; for(z=1;z<=n;z++) { cin>>s1>>s2; l1=s1.length(); l2=s2.length(); for(i=0;i<l1;i++) { a=s1[i]-'0'; b=a; for(j=0;j<i;j++) { b=b*10; } s=s+b; } for(i=0;i<l2;i++) { a=s2[i]-'0'; b=a; for(j=0;j<i;j++) { b=b*10; } s=s+b; } m[0]=s/1000;m[1]=s%1000/100;m[2]=s%100/10;m[3]=s%10; for(i=0;;) { if(m[i]==0) i++; if(m[i]!=0) {l=4-i;break;} } for(i=0;i<l;i++) { s3[i]=m[4-i-1]+'0'; } for(i=0;i<l;i++) { if((s3[i]=='0')&&(k==i)) k++; else cout<<s3[i]; } if(z!=n) cout<<endl; s=0; } return 0;}
0 0
- 编程练习2
- 初学者编程练习2
- 编程进阶练习2
- c++编程练习2
- 编程练习(2)
- 编程小练习(2)
- 编程练习2
- 编程练习(2)
- 编程小练习2
- 编程练习(2)
- 编程练习2
- 线程编程练习2
- 编程练习2
- 编程练习(2)
- 网络编程的练习2
- java基础编程练习2
- python核心编程-练习2
- 第十章编程练习(2)
- 泛型-接口泛型
- Leetcode: 股票买卖大全
- 图解kset,kobject,subsys
- ini配置文件格式
- 虚析构函数
- 编程练习2
- 关于codeblocks左边文件栏不见的问题
- 算法导论 练习题 3.2-8
- Unity3d 控制物体transform移动的几种方法
- Java实现正向最大匹配法和逆向最大匹配法
- Tomcat配置环境变量(Window系统)
- FPGA视频处理
- jQuery学习(二)-常规选择器
- 快速排序算法