AC
2024-10-06 10:49:22
发布于:北京
5阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
string fun(string a, string b){
string res = "";
if (a.size() < b.size() || a.size() == b.size() && a < b){
swap(a, b);
res += '-';
}
int la = a.size(), lb = b.size();
vector<int> A(la+10), B(la+10), C(la+10);
for (int i=0;i<la;i++) A[i] = a[la-i-1]-'0';
for (int i=0;i<lb;i++) B[i] = b[lb-i-1]-'0';
for (int i = 0; i < la; i++){
C[i] += A[i] - B[i];
if (C[i] < 0){
C[i+1]--;
C[i]+=10;
}
}
int len = la-1;
while (C[len]==0&&len>0) len--;
for (int i=len;i>=0;i--){
res += C[i]+'0';
}
return res;
}
int main(){
string a, b;
cin >> a >> b;
cout << fun(a, b);
return 0;
}
这里空空如也
有帮助,赞一个