提交时间:2023-10-10 13:27:27
运行 ID: 106195
#include <bits/stdc++.h> using namespace std; string aa,bb; bool jw[114514]; int a[114514],b[114514],ans[114514],len1,len2,n; int comp(string x, string y)//比较大小 { if(x.length()>y.length())return 1; if(x.length()<y.length())return 0; else { for(int i = 0; i < x.length(); i++) { if(x[i]>y[i]) { return 1; } if(x[i]<y[i]) { return 0; } } } return 2; } void init() { cin >> aa >> bb; int lhcsb = comp(aa,bb); if(lhcsb == 2)//一样就输出0 { cout << 0 << endl; exit(0); } else if(lhcsb == 0)//处理负数 { swap(aa,bb); cout << '-'; } len1 = aa.length();len2 = bb.length(); if(len2<len1)//前面加0 { string tmp=""; for(int i = 1; i <= len1-len2; i++)tmp+='0'; bb = tmp+bb; } len2 = n = len1; //反过来存 for(int i = 1; i <= len1; i++)a[i] = aa[len1-i]-'0'; for(int i = 1; i <= len2; i++)b[i] = bb[len2-i]-'0'; } signed main() { init(); for(int i = 1; i <= n; i++) { a[i]-=jw[i];//处理借位 if(a[i]<0)//小于0再借 { a[i] = 9; jw[i+1] = 1; } if(a[i]>=b[i])//算 { ans[i] = a[i]-b[i]; } else//如果减不了,那就借位 { ans[i] = a[i]+10-b[i]; jw[i+1] = 1; } } bool flag = false; for(int i = n; i >= 1; i--)//注意存的时候是反过来的 { if(ans[i]&&!flag)//处理前导零 例如答案是0123,我们只输出123 { flag = true; } if(flag) { cout << ans[i]; } } if(!flag)//其实可以不用,上面init已经处理过了 { cout << 0; } return 0; }