提交时间:2023-10-13 13:58:57
运行 ID: 106589
#include<bits/stdc++.h> using namespace std; int a1[50005] , b1[50005] , c[50005]; inline void AaddB(string a,string b) { int lena = a.size(); int lenb = b.size(); for(int i = 0;i < lena;i++) a1[lena - i] = a[i] - 48; for(int i = 0;i < lenb;i++) b1[lenb - i] = b[i] - 48; int lenc = 1; int x = 0; while(lenc <= lena || lenc <= lenb) { c[lenc] = a1[lenc] + b1[lenc] + x; x = c[lenc] / 10; c[lenc] %= 10; lenc++; } c[lenc] = x; if(c[lenc] == 0) lenc--; for(int i = lenc;i >= 1;i--) cout << c[i]; cout << endl; return; } inline void AsubtrackB(string a1,string b1) { if(a1.length() < b1.length() || a1.length() == b1.length() && a1 < b1) { swap(a1 , b1); cout << '-'; } int lena = a1.size(); int lenb = b1.size(); int lenc = max(lena,lenb); for(int i = 1;i <= lena;i++) a1[i] = a1[lena - i] - 48; for(int i = 1;i <= lenb;i++) b1[i] = b1[lenb - i] - 48; for(int i = 1;i <= lenc;i++) { if(a1[i] < b1[i]) { a1[i + 1]--; a1[i] += 10; } c[i] = a1[i] - b1[i]; } while(!c[lenc] && lenc > 1) lenc--; for(int i = lenc;i >= 1;i--) cout << c[i]; } inline void AmultiplyB(string n,string m) { int lenn,lenm; lenn = n.size(); lenm = m.size(); for(int i = lenn - 1;i >- 1;i--) { for(int j = lenm - 1;j >- 1;j--) { int tmp1 = n[i] - '0' + 0; int tmp2 = m[j] - '0' + 0; int tmp1multmp2 = tmp1 * tmp2; if(a1[lenm + lenn - i - j - 1] == -1) a1[lenm + lenn - i - j - 1] = tmp1multmp2; else a1[lenm + lenn - i - j - 1] += tmp1multmp2; } } for(int i = 1;i <= 5000;i++) { if(a1[i] == -1) break; if(a1[i] >= 9) { if(a1[i + 1] == -1) a1[i+1]=a1[i] / 10; else a1[i + 1] += a1[i] / 10; a1[i] %= 10; } } bool flag = true; for(int j = 5000;j >= 1;j--) { if(a1[j] == 0) a1[j] =- 1; else break; } for(int i = 5000;i >= 1;i--) { if(a1[i] != -1) { cout<<a1[i]; flag = false; } } if(flag) cout << 0;//乘法 } inline void csh() { memset(a1,0,sizeof(a1)); memset(b1,0,sizeof(b1)); memset(c,0,sizeof(c)); } int main() { string a,b;cin>>a>>b; csh();AaddB(a,b); csh();AsubtrackB(a,b);cout<<endl; memset(a1,-1,sizeof(a1));AmultiplyB(a,b); return 0; }