poj 1504 Adding Reversed Numbers(简单字符串的处理)
2014-07-16 17:03
369 查看
Adding Reversed Numbers
点击打开题目链接
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld
& %llu
SubmitStatus
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).
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
将两数反向相加,在反向输出;
代码:
点击打开题目链接
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld
& %llu
SubmitStatus
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).
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<string.h> using namespace std; void deletezeor(char s[])//去除前导零 { int len=strlen(s),i,j,k; if(s[0]=='0') { for(i=0;i<len;i++) { if(s[i]!='0') break; } for(k=0,j=i;j<len;j++,k++) { s[k]=s[j]; } s[k]='\0'; } } void add(char a[],char b[],char s[])//想加 { int len1=strlen(a); int len2=strlen(b),c,i,k,d; c=0;i=0;k=0; if(len1>=len2) { while(b[i]!='\0') { d=a[i]-'0'+b[i]-'0'+c; if(d>9) { s[k++]=d%10+'0'; c=1; } else {s[k++]=d+'0';c=0;} i++; } while(a[i]!='\0') { d=a[i]-'0'+c; if(d>9) { s[k++]=d%10+'0'; c=1; } else {s[k++]=d+'0';c=0;} i++; } if(c==1) s[k++]='1'; } else { while(a[i]!='\0') { d=a[i]-'0'+b[i]-'0'+c; if(d>9) { s[k++]=d%10+'0'; c=1; } else {s[k++]=d+'0';c=0;} i++; } while(b[i]!='\0') { d=b[i]-'0'+c; if(d>9) { s[k++]=d%10+'0'; c=1; } else {s[k++]=d+'0';c=0;} i++; } if(c==1) s[k++]='1'; } deletezeor(s); } int main() { int t; char a[1000],b[1000],s[1000]; while(cin>>t) { while(t--) { memset(a,'\0',sizeof(a)); memset(b,'\0',sizeof(b)); memset(s,'\0',sizeof(s)); cin>>a>>b; add(a,b,s); cout<<s<<endl;//输出 } } return 0; }
相关文章推荐
- Poj 1504 Adding Reversed Numbers(用字符串反转数字)
- POJ 1504 Adding Reversed Numbers(字符串巧解)
- Poj 1504 Adding Reversed Numbers(用字符串反转数字)
- poj 1504 Adding Reversed Numbers
- POJ 1504 adding reversed numbers
- poj 1504 Adding Reversed Numbers【反转数字】
- POJ 1504 Adding Reversed Numbers(水~)
- POJ 1504 Adding Reversed Numbers…
- POJ 1504 Adding Reversed Numbers
- POJ 1504,ZOJ 2001,UVA 713, Adding Reversed Numbers,错误,已找到错误
- UVA713 UVALive5539 POJ1504 ZOJ2001 Adding Reversed Numbers
- POJ 1504 Adding Reversed Numbers
- POJ 1504:Adding Reversed Numbers
- (数组的应用二:高精度运算4.2.1)POJ 1504 Adding Reversed Numbers(将一个数取反)
- ZOJ2001 POJ1504 Adding Reversed Numbers
- POJ1504-Adding Reversed Numbers(C语言实现)
- poj 1504 Adding Reversed Numbers
- POJ 1504 Adding Reversed Numbers
- POJ-1504 Adding Reversed Numbers-逆序数相加
- POJ 1504,ZOJ 2001,UVA 713, Adding Reversed Numbers,错误,已找到错误