View Code of Problem 3689

#include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<string>
#include<cmath>
#include<unordered_map>
#include<stack>

using namespace std;

bool judge(string& a, string& b) {

	if (a.size() < b.size())
		return true;
	if (a.size() > b.size())
		return false;

	for (int i = 0; i < a.size(); i++) {

		if (a[i] < b[i])
			return true;
	}

	return false;
}


int main()
{
	int T;
	cin >> T;

	for (int i = 0; i < T; i++) {

		string a, b;
		cin >> a >> b;	//不能用getline(), 吐了

		int flag = 0;

		if (judge(a, b)) {

			flag = 1;
			while (a.size() < b.size()) {

				a = '0' + a;
			}
		}

		
		if (flag == 1)
			swap(a, b);

		while (b.size() < a.size()) {

			b = '0' + b;
		}

		int pos = a.size() - 1;
		string res;
		int jie = 0;
		while (pos >= 0) {

			int sub = (a[pos] - '0') - (b[pos] - '0');

			if (jie == 1) {

				sub--;
				jie = 0;
			}

			if (sub < 0) {

				sub += 10;
				jie = 1;
			}

			res = to_string(sub) + res;

			pos--;
		}

		cout << "Case #" << i + 1 << ":" << endl;

		if (res.size() > 1) {

			int pos = 0;
			while (res[pos] == '0')
				pos++;

			if (pos < res.size())
				res = res.substr(pos, res.size() - pos);
			else
				res = '0';
		}

		if (flag == 1)
			cout << '-' << res << endl;
		else
			cout << res << endl;
	}
}

Double click to view unformatted code.


Back to problem 3689