2 条题解
-
5
#include <bits/stdc++.h> using namespace std; bool cmp(vector<int> &A, vector<int> &B) //判断是否有 A>=B { if (A.size() != B.size()) return A.size() > B.size(); for (int i = A.size() - 1; i >= 0; i--) //比较首位 { if (A[i] != B[i]) return A[i] > B[i]; } return true; } vector<int> sub(vector<int> &A, vector<int> &B) { vector<int> C; int t = 0; //借位 for (int i = 0; i < A.size(); i++) { t = A[i] - t; if (i < B.size()) t -= B[i]; C.push_back((t + 10) % 10); //二合一 t>=0 t=t:不需要借位;t<0 t=t+10:需要借位 if (t < 0) t = 1; else t = 0; } //去除高位0 while (C.size() > 1 && C.back() == 0) C.pop_back(); return C; } int main() { string a, b; cin >> a >> b; vector<int> A, B; for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0'); for (int i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0'); vector <int> C; if (cmp(A, B)) C = sub(A, B); //保证 A >= B; else C = sub(B, A),cout << '-'; for (int i = C.size() - 1; i >= 0; i--) cout << C[i]; return 0; }
求求了,点个赞吧!
-
1
#include <bits/stdc++.h> using namespace std; string s1,s2; int a[11000000],b[11000000],c[11000000],len,p; int i; char f='+'; int main(){ cin>>s1>>s2; if(s1.size()<s2.size()||(s1.size()==s2.size()&&s1<s2)){ f='-'; swap(s1,s2); } for(i=0;i<s1.size();i++){ a[i]=s1[s1.size()-i-1]-'0'; } for(i=0;i<s2.size();i++){ b[i]=s2[s2.size()-i-1]-'0'; } len=s1.size(); for(i=0;i<len;i++){ if(a[i]<b[i]){ a[i+1]=a[i+1]-1; a[i]=a[i]+10; } c[i]=a[i]-b[i]; } if(f=='-') cout<<f; for(i=len-1;i>=0;i--){ if(c[i]!=0){ p=i; break; } } for(i=p;i>=0;i--){ cout<<c[i]; } return 0; }
- 1
信息
- ID
- 268
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 6
- 标签
- 递交数
- 290
- 已通过
- 85
- 上传者