#include<bits/stdc++.h>
using namespace std;
string s1,s2;
int a[250],b[250],c[250];
char f = '+';
int len,p;
int main(){
freopen("sub.in","r",stdin);
freopen("sub.out","w",stdout);
cin >> s1 >> s2;
if (s2.size() > s1.size() || (s1.size() == s2.size() && s2 > s1)) {
swap(s1,s2);
f = '-';
}
for (int i = 0; i < s1.size(); i++) {
a[s1.size() - i - 1] = s1[i] - '0';
}
for (int i = 0; i < s2.size(); i++) {
b[s2.size() - i - 1] = s2[i] - '0';
}
len = s1.size();
for (int i = 0; i < len; i++) {
if (a[i] < b[i]) {
a[i + 1] -= 1;
a[i] += 10;
}
c[i] = a[i] - b[i];
}
if (f == '-') cout << f;
for (int i = len - 1; i >= 0; i--) {
if (c[i] != 0) {
p = i;
break;
}
}
for (int i = p; i >= 0; i--) cout << c[i];
}
这道题用三个数组就行。
第一步:把输入的字符串转换成数组(因为都是数字)
第二步:相减,不够就借位
第三步:判断是否是负数
第四步:输出