Need help
查看原帖
Need help
1165586
lizishi楼主2025/1/19 20:15
#include <iostream>
#include <cstring>

#define maxn 2000000
#define need return
#define help 0

using namespace std;

char str_a[maxn] = {0}, str_b[maxn] = {0};
int a[maxn], b[maxn], ans[maxn];

int max_index(int x[], int y[])  {
	for (int i = 0; i < maxn; i++) {
		if (x[i] > y[i]) {
			return 1;
		} else if (y[i] > x[i]) {
			return 2;
		}
	}
	return 0;
}

int main() {
	cin >> str_a >> str_b;
	int alen = strlen(str_a);
	int blen = strlen(str_b);
	for (int i = 0; i < maxn; i++) {
		if (str_a[i]) a[maxn - alen + i] = str_a[i] - '0';
		if (str_b[i]) b[maxn - blen + i] = str_b[i] - '0';
	}
	if (max_index(a, b) == 2) {
		cout << '-';
		int temp;
		for (int i = 0; i < maxn; i++) {
			temp = a[i];
			a[i] = b[i];
			b[i] = temp;
		}
	}
	for (int i = maxn - 1; i >= 0; i--) {
		if ((a[i] < b[i]) || ((a[i] == b[i]) && (ans[i] == -1))) {
			ans[i] += 10;
			a[i - 1]--;
		}
		ans[i] += (a[i] - b[i]);
	}
	bool is0 = true;
	for (int i = 0; i < maxn; i++) {
		if (ans[i]) {
			is0 = false;
			break;
		}
	}
	if (is0) {
		cout << 0;
		return 0;
	} else {
		for (int i = 0; i < maxn; i++) {
			if (i == 0) {
				while (ans[i] == 0) {
					i++;
				}
			}
			cout << ans[i];
		}
	}
	need help;
}
2025/1/19 20:15
加载中...