提交时间:2024-11-08 07:19:48
运行 ID: 55534
#include <bits/stdc++.h> using namespace std; void sub(int a[], int b[], int c[], int len) { for (int i = 0; i < len; i++) { if (a[i] < b[i]) { a[i] += 10; a[i + 1]--; } c[i] = a[i] - b[i]; } } bool judge(int a[], int b[], int len) { for (int i = len; i >= 0; i--) { if (a[i] < b[i]) return false; // if (a[i] > b[i]) return true; } return true; } int main() { char a[105] = {}, b[105] = {}; cin >> a >> b; int c[106] = {0}, d[106] = {0}; int smt[105] = {0}; for (int i = 0; i < strlen(a); i++) { c[strlen(a) - 1 - i] = a[i] - '0'; } for (int i = 0; i < strlen(b); i++) { d[strlen(b) - 1 - i] = b[i] - '0'; } if (judge(c, d, 104)) sub(c, d, smt, strlen(a)); else { cout << '-'; sub(d, c, smt, strlen(b)); } bool f = true; for (int i = 104; i >= 0; i--) { if (smt[i] != 0 || i == 0) f = false; if ((smt[i] == 0 && !f ) || smt[i] != 0) cout << smt[i]; } return 0; }