View Code of Problem 3689

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(void)
{
    string str1, str2;
    int T;
    cin >> T;
    getchar();
    for (int i = 1; i <= T; i++)
    {
        int flag = 1;
        cin >> str1;
        cin >> str2;
        if (str1.size() < str2.size() || (str1.size() == str2.size() && str1 < str2))
        {
            swap(str1, str2);
            flag = 0;
        }
        reverse(str1.begin(), str1.end());
        reverse(str2.begin(), str2.end());
        for (int j = 0; j < str2.size(); j++)
        {
            int substractIndex = j;
            if (str1[j] >= str2[j])
            {
                str1[j] = str1[j] - str2[j] + '0';
            }
            else
            {
                str1[j] = (str1[j] - '0' + 10) - (str2[j] - '0') + '0';
                substractIndex++;
                while (str1[substractIndex] == '0' && substractIndex < str1.size())
                {
                    str1[substractIndex++] = '9';
                }

                str1[substractIndex]--;
            }
        }
        reverse(str1.begin(), str1.end());
        while (str1.size() > 1 && str1[0] == '0')
            str1.erase(str1.begin());
        cout << "Case #" << i << ":" << endl;
        if (!flag)
            cout << "-";
        cout << str1 << endl;
    }
}

Double click to view unformatted code.


Back to problem 3689