ACM (TJU) : 2149


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

char a [100], b [100];

void reset ()
{
    for ( int i = 0; i < 100; i++ )
    a [i] = b [i] = '0';
}

void reverse ()
{
    int length = strlen (a) - 1;

    for ( int i = 0; i <= (length/2); i++ )
    swap (a [i], a [length - i]);

    length = strlen (b) - 1;
    for ( int i = 0; i <= (length/2); i++ )
    swap (b [i], b [length - i]);
}

int main ()
{
    int dataSet;
    int testCase = 0;
    cin >> dataSet;

    while ( dataSet-- ) {

        reset ();

        cin >> a >> b;
        printf ("%d ", ++testCase);
        reverse ();
        int length;

        if ( strlen (a) > strlen (b) )
            length = strlen (a);
        else
            length = strlen (b);

        a [strlen (a)] = '0';
        b [strlen (b)] = '0';


        int carry = 0;
        int temp;
        char res [100];
        int index = 0;

        for ( int i = 0; i < length; i++ ) {
            temp = (a [i] - 48) + (b [i] - 48) + carry;
            if ( temp == 2 ) {
                carry = 1;
                res [index++] = '0';
            }

            else if ( temp == 3 ) {
                carry = 1;
                res [index++] = '1';
            }

            else {
                carry = 0;
                res [index++] = temp + 48;
            }
        }

        if ( carry == 1 )
        res [index++] = carry + 48;

        bool flag = false;

        for ( int i = index - 1; i >= 0; i-- ) {
            if ( res [i] == '1' || flag ) {
                printf("%c", res [i]);
                flag = true;
            }
        }

        if ( flag == false )
        printf ("0");
        printf ("\n");

    }

    return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s