1453: Adding Reversed Numbers
2010-03-15 12:29
197 查看
1453: Adding Reversed Numbers
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
Sample Output
#include<stdio.h>
#include<string.h>
const int max=200;
void reverse(char *p)
{
int i,j;
char t;
int n=strlen(p);
for(i=0,j=n-1;i<j;i++,j--)
{
t=*(p+i);
*(p+i)=*(p+j);
*(p+j)=t;
}
}
int main()
{
int n,i,j;
scanf("%d",&n);getchar();
while(n--)
{
char a[max+1],b[max+1];
scanf("%s",a);getchar();
scanf("%s",b);getchar();
reverse(a);
reverse(b);
int f[max+1]={0},g[max+1]={0},z[max+1]={0};
int l=max;
for(i=strlen(a)-1;i>=0;i--)
f[l--]=a[i]-'0';
l=max;
for(i=strlen(b)-1;i>=0;i--)
g[l--]=b[i]-'0';
for(i=max;i>=0;i--)
{
z[i]+=(f[i]+g[i]);
if(z[i]>=10)
{
z[i-1]+=z[i]/10;
z[i]%=10;
}
}
for(i=0;i<max+1;i++)//503 497
if(z[i]) {j=i;break;}
int flag=0;
for(i=max;i>=j;i--)
{
if(z[i]) flag=1;
if(flag)
printf("%d",z[i]);
}
printf("/n");
}
return 0;
}
Status | In/Out | TIME Limit | MEMORY Limit | Submit Times | Solved Users | JUDGE TYPE |
---|---|---|---|---|---|---|
stdin/stdout | 3s | 8192K | 815 | 260 | Standard |
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<stdio.h>
#include<string.h>
const int max=200;
void reverse(char *p)
{
int i,j;
char t;
int n=strlen(p);
for(i=0,j=n-1;i<j;i++,j--)
{
t=*(p+i);
*(p+i)=*(p+j);
*(p+j)=t;
}
}
int main()
{
int n,i,j;
scanf("%d",&n);getchar();
while(n--)
{
char a[max+1],b[max+1];
scanf("%s",a);getchar();
scanf("%s",b);getchar();
reverse(a);
reverse(b);
int f[max+1]={0},g[max+1]={0},z[max+1]={0};
int l=max;
for(i=strlen(a)-1;i>=0;i--)
f[l--]=a[i]-'0';
l=max;
for(i=strlen(b)-1;i>=0;i--)
g[l--]=b[i]-'0';
for(i=max;i>=0;i--)
{
z[i]+=(f[i]+g[i]);
if(z[i]>=10)
{
z[i-1]+=z[i]/10;
z[i]%=10;
}
}
for(i=0;i<max+1;i++)//503 497
if(z[i]) {j=i;break;}
int flag=0;
for(i=max;i>=j;i--)
{
if(z[i]) flag=1;
if(flag)
printf("%d",z[i]);
}
printf("/n");
}
return 0;
}
相关文章推荐
- zoj2001 Adding Reversed Numbers
- Adding Reversed Numbers(0058)
- zoj 2001 Adding Reversed Numbers(大水水一个~)
- POJ 1504 Adding Reversed Numbers
- ZOJ 2001 Adding Reversed Numbers
- 2001_Adding Reversed Numbers(字符串类型数求和)
- POJ 1504 Adding Reversed Numbers…
- Poj 1504 Adding Reversed Numbers(用字符串反转数字)
- POJ 1504 Adding Reversed Numbers (水题,高精度整数加法)
- poj 1504 Adding Reversed Numbers【反转数字】
- zoj 2001 Adding Reversed Numbers
- POJ 1504 Adding Reversed Numbers(字符串巧解)
- Adding Reversed Numbers(关于pow函数出错)
- 4.2.1 B - Adding Reversed Numbers(高精度运算)
- ZOJ Problem Set - 2001 Adding Reversed Numbers
- 《数据结构编程实验》 4.2.1Adding Reversed Numbers
- zoj 2001 Adding Reversed Numbers
- ZOJ2001 POJ1504 Adding Reversed Numbers
- poj 1504 Adding Reversed Numbers
- (高精度) Adding Reversed Numbers (P1504)