B - Adding Reversed Numbers

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

做题感想:因为用memeset()函数写错了格式导致wa了好几次,郁闷!正确格式为:假如开辟了一个数组a[100];要对其赋初值为0,memset(a,0,sizeof(a));

法一思路:对输入的两个数拆开装入数组,然后逆序,再按竖式相加求其和,把前面的零去掉。然后逆序输出。逆序输出时前面的零都不要输出。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
    int t,n,m;
    cin>>t;
    int a[10000]={0};
    int b[10000]={0};
    int c[20000];
    for(int i=0;i<t;i++)
    {
        cin>>n>>m;
        int n1=n;
        int m1=m;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        int k1=0,k2=0;
        while(n)
        {
            a[k1]=n%10;
            n=n/10;
            k1++;
        }
        while(m)
        {
            b[k2]=m%10;
            m=m/10;
            k2++;
        }
        int i1,i2;
        for(i1=0;i1<k1/2;i1++)
        {
            swap(a[i1],a[k1-i1-1]);
        }
        for(i2=0;i2<k2/2;i2++)
        {
            swap(b[i2],b[k2-1-i2]);
        }
        int l;
        l=(k1>k2)?k1:k2;
        int h=0,temp,p;
        for(p=0;p<l;p++)
        {
            temp=a[p]+b[p]+h;
            c[p]=temp%10;
            h=temp/10;
        }
        while(h)
        {
            c[p]=h%10;
            h=h/10;
            p++;
        }
        int p1=0,i5=p-1;
    //    for(i5=p-1;i5>=0;i5--)
        //{
        if(n1==0&&m1==0)
            cout<<0<<endl;
        else{
        while(i5)
        {
            if(c[i5])
                break;
            p1++;
            i5--;
        }
      
      int i4=0;
        while(1)
        {
            if(c[i4])
                break;
            i4++;
        }
        for(;i4<p-p1;i4++)
            cout<<c[i4];
        cout<<endl;
        }
    }
    return 0;
}
法二:

对输入的的每一个数利用纯数学思想逆序,得到的两个数相加。不用考虑前导0的情况。

代码:

#include<iostream>
using namespace std;
int f(int mm)
{
    int p=0,i;
    for(i=0;;i++)
    {
        p=p*10+mm%10;//对数m进行逆序
        mm=mm/10;
        if(mm==0)
            break;
    }
    return p;
}
int main()
{
    int i,n,t,m,sum;
    cin>>t;
    for(i=0;i<t;i++)
    {
        cin>>n>>m;
        sum=f(f(n)+f(m));//逆序后的两个数相加
        cout<<sum<<endl;
    }
}


你可能感兴趣的:(B - Adding Reversed Numbers)