Uva 10013 Solution

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
    string a, b, c;
    int n, m, x, y, t, k, i, d, j;

    while(cin >> x)
    {
        for(k=0; k<x; k++)
        {
            cin >> t;
            j = t;
            while(t--)
            {
                cin >> n >> m;
                a.push_back(n+48);
                b.push_back(m+48);
            }
            reverse(a.begin(), a.end());
            reverse(b.begin(), b.end());
            i = 0;
            n = m = y = 0;
            while(i<j)
            {
                n = a[i] - 48;
                m = b[i] - 48;
                d = n + m + y;
                c.push_back((d%10)+48);
                y = d/10;
                i++;
            }
            if(y>0)
            {
                c.push_back(y+48);
            }
            reverse(c.begin(), c.end());
            if(k!=0)
            {
                cout << endl;
            }
            cout << c << endl;
            a.clear();
            b.clear();
            c.clear();
        }
    }
    return 0;
}

Comments