ZOJ Problem Set - 2001 Adding Reversed Numbers

来源:互联网 发布:微信电子相册软件 编辑:程序博客网 时间:2024/04/19 17:16
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


Source: Central Europe 1998




分析:

字符串处理题。

题意:

输入多组数据,每组数据两个整数(可能很大,超过long long),要求把两个数反转后再加和,再把和反转。




仔细分析发现:反转两次,其实正好可以用string来处理,因为这时候低位与高位的顺序正好是string方便处理的形式。用string处理,可以不反转。

同时这个题也可以作为两个大数相加的模板。



ac代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
string sa,sb,st,s;
int main()
{
    int n,i,j;
    scanf("%d",&n);
    while(cin>>sa>>sb)
    {
        if(sa.length()<sb.length())
        {
            st=sa;
            sa=sb;
            sb=st;
        }
        //reverse(sa.begin(),sa.end());
        //reverse(sb.begin(),sb.end());
        int La=sa.length();
        int Lb=sb.length();
        //printf("sa.length=%d\n",La);
        //printf("sb.length=%d\n",Lb);
        int sum=0,p=0;
        for(i=0;i<La;i++)
        {
            if(i<Lb)
            {
                sum=sa[i]+sb[i]-96+p;
                if(sum>=10)
                {
                    p=1;
                    sum%=10;
                }
                else
                p=0;//把p(进位)重新设置为0
                s+=char(sum+48);//字符串string的正确添加方式
                //s[i]=char(sum+48);//字符串string的错误添加方式,此时s.length()永远等于0
                //printf("%c",s[i]);
            }
            else
            {
                sum=sa[i]-48+p;
                 if(sum>=10)
                {
                    p=1;
                    sum%=10;
                }
                else p=0;//把p(进位)重新设置为0
                s+=char(sum+48);
               // printf("%c",s[i]);
            }
        }
        if(p)
        s+=char(1+48);
        int Ls=s.length();
        //reverse(s.begin(),s.end());
        bool flag=1;
        for(i=0;i<Ls;i++)
        {
            if(s[i]=='0'&&flag)//这里注意是判断s[i]=='0',而不是s[i]==0.是个坑。
                continue;
           else
            {
                flag=0;
                cout<<s[i];
            }
        }
        cout<<endl;
        s="";
    }
    return 0;
}




0 0
原创粉丝点击