B - Adding Reversed Numbers(4.2.1)

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

描述:将输入的数字倒序相加,再将其和倒序输出。

这道题看起来挺简单,做起来很复杂。其实是我的方法复杂了。通过比较发现,有很多方法。这里介绍三种。

第一种:利用库函数reverse()逆置函数将变量逆置,很简单。具体使用方法百度。

也可以自己写一个逆置函数,非常简单。下面我提供的便是我自己写的。

第二种:利用数组的方法

第一种方法:reverse()逆置函数(自己写的)

#include <iostream>

using namespace std;

template <typename T>
T reverse( T k )   //反转函数
{
    T m = 0;
    while(k)
    {
        m = m * 10 + k % 10;
        k /= 10;
    }
    return m;
}
int main()
{
    int n, j, k;
    cin >> n;
    while( n-- )
    {
        cin >> j >> k;
        cout << reverse( reverse(j) + reverse(k) ) << endl;  //两个数分别反转 相加所得结果再反转
    }
    return 0;
}


 

 

第二种:数组的方法:


#include <iostream>
#include <cstring>
using namespace std;

void reversal( int * , int );
void add( int *p, int *q , int *s)    //两数从低位到高位依次相加
{
    int i;
    for( i = 9; i >= 0; i-- )
    {
        s[i] = s[i] + p[i] + q[i];
        if( s[i] > 9 )     //产生进位
        {
            s[i] %= 10;
            s[i-1] += 1;
        }
    }
}

void trans( int *p, int n)   //将变量转换到数组
{
    int i = 0, l;
    while( n != 0 )
    {
        p[i++] = n % 10;
        n /= 10;
    }
    l = i;
    reversal( p, l );
    for( ; i < 10; i++ )
        p[i] = 0;
}

void reversal( int *p, int l )    //逆序
{
    int i, t;
    for(i = 0; i < l/2; i++ )
    {
        t = p[i];
        p[i] = p[l-i-1];
        p[l-i-1] = t;
    }
}
int main()
{
    int N,x,y,i,l;
    int a[10], b[10],c[20];
    cin >> N;
    while( N-- )
    {
        for( i = 0; i < 10; i++ )
            a[i] = b[i] = c[i] = 0;
        cin>>x>>y;
        trans( a, x );
        trans( b, y );
        /*for( i = 0; i < 10; i++ )
            cout<<a[i];
        cout<<endl;
        for( i = 0; i < 10; i++ )
            cout<<b[i];
        cout<<endl;
        //测试转换成功。。。*/

        reversal( a, 10 );
        reversal( b, 10);

        /*for( i = 0; i < 10; i++ )
            cout<<a[i];
        cout<<endl;
        for( i = 0; i < 10; i++ )
            cout<<b[i];
        cout<<endl;
        //测试反转成功。。。。*/

        add( a, b, c);

        /*for( i = 0; i < 10; i++ )
            cout<<c[i];
        cout<<endl;
        //测试相加成功。。。*/
        i = 0;
        while( c[i] == 0 ) c[i++] = -1; //从前向后查找将前面所有的零置为-1
        i = 9;
        while( c[i] == 0 ) c[i--] = -1;//从后向前查找将前面所有的零置为-1
        for(i = 9; i >= 0; i-- )
            if( c[i] != -1 )   //逆序将所有非负的数字输出
                cout<<c[i];
        cout<<endl;
    }
    return 0;
}
<span style="font-family:Arial;BACKGROUND-COLOR: #ffffff"></span>

你可能感兴趣的:(数据结构,C++,Reversed,Adding,numb)