高精度减
2023-08-05 17:16:26
发布于:浙江
#include<iostream>
#include<string>
using namespace std;
const int N = 250 + 10;
int a[N],b[N],c[N];
string s1,s2;
int main() {
cin >> s1 >> s2;
if(s1.size() < s2.size() || s1.size() == s2.size() && s1 < s2) {
swap(s1,s2);
cout << '-';
}
for(int i = 0; i < s1.size(); i++) {
a[i] = s1[s1.size() - 1 - i] - '0';
}
for(int i = 0; i < s2.size(); i++) {
b[i] = s2[s2.size() - 1 - i] - '0';
}
int l = max(s1.size(),s2.size());
for(int i = 0; i < l; i++) {
c[i] += a[i] - b[i];
if(c[i] < 0) {
c[i+1]--;
c[i]+=10;
}
}
int t = s1.size() - 1;
while(c[t] == 0&& t>0) {
t--;
}
for(int i = t; i >= 0; i--) {
cout << c[i];
}
return 0;
}
这里空空如也
有帮助,赞一个