High precision operation
1. High precision addition
Given two positive integers, sum them.
Input format
There are two lines, each containing an integer.
Output format
A total of one line containing the sum of the requirements.
Data range
1 ≤ integer length ≤ 100000
Input example:
12
23
Output example:
35
#include<iostream> #include<algorithm> #include<vector> using namespace std; typedef long long LL; vector<int> a,b,res; string m,n; int main() { cin >> m >> n; for(int i=m.size()-1;i>=0;i--) a.push_back(m[i]-'0'); for(int i=n.size()-1;i>=0;i--) b.push_back(n[i]-'0'); int t=0; int i=0; while(i<a.size()||i<b.size()||t) { if(i<a.size()) t+=a[i]; if(i<b.size()) t+=b[i]; res.push_back(t%10); if(t>=10) t=1; else t=0; i++; } for(int i=res.size()-1;i>=0;i--) printf("%d",res[i]); return 0; }
2. High precision subtraction
Given two positive integers, calculate their difference, and the result may be negative.
Input format
There are two lines, each containing an integer.
Output format
A total of one line, including the difference.
Data range
1 ≤ integer length ≤ 105
Input example:
32
11
Output example:
21
#include<iostream> #include<vector> #include<algorithm> using namespace std; string a,b; vector<int> m,n,res; bool judge(vector<int> a,vector<int> b) { if(a.size()==b.size()) { for(int i=0;i<a.size();i++) if(a[i]!=b[i]) return a[i]>b[i]; } else return a.size()>b.size(); return true; } void minu(vector<int> a,vector<int> b) { int t=0; for(int i=0;i<a.size();i++) { t=a[i]-t; if(i<b.size()) t-=b[i]; res.push_back((t+10)%10); if(t<0) t=1; else t=0; } while(res.size()>1&&res.back()==0) res.pop_back(); } int main() { cin>> a >> b; for(int i=a.size()-1;i>=0;i--) m.push_back(a[i]-'0'); for(int i=b.size()-1;i>=0;i--) n.push_back(b[i]-'0'); if(!judge(m,n)) { printf("-"); minu(n,m); } else minu(m,n); for(int i=res.size()-1;i>=0;i--) printf("%d",res[i]); return 0; }
3. High precision multiplication
Given two positive integers A and B, please calculate the value of A * B.
Input format
There are two lines in total. The first line contains the integer A, and the second line contains the integer B.
Output format
A total of one line, containing the value of A * B.
Data range
1 ≤ A length ≤ 100000,
1≤B≤10000
Input example:
2
3
Output example:
6
#include<iostream> #include<vector> #include<algorithm> using namespace std; vector<int> a,res; int n; string m; void mul(vector<int> a,int b) { int t=0; for(int i=0;i<a.size();i++) { t+=a[i]*b; res.push_back(t%10); t/=10; } while(t) { res.push_back(t%10); t/=10; } } int main() { cin >> m >> n; for(int i=m.size()-1;i>=0;i--) a.push_back(m[i]-'0'); mul(a,n); for(int i=res.size()-1;i>=0;i--) printf("%d",res[i]); return 0; }
4. High precision Division
Given two positive integers A and B, please calculate the quotient and remainder of A / B.
Input format
There are two lines in total. The first line contains the integer A, and the second line contains the integer B.
Output format
There are two lines in total. The first line outputs the quotient and the second line outputs the remainder.
Data range
1 ≤ A length ≤ 100000,
1≤B≤10000
Input example:
7
2
Output example:
3
1
#include<iostream> #include<vector> #include<algorithm> using namespace std; vector<int> a,res; string m; int n,t; void divide(vector<int> a,int b) { t=0; for(int i=0;i<a.size();i++) { t=t*10+a[i]; res.push_back(t/b); t=t%b; } reverse(res.begin(),res.end()); while(res.size()>1&&res.back()==0) res.pop_back(); } int main() { cin >> m >> n; for(int i=0;i<m.size();i++) a.push_back(m[i]-'0'); divide(a,n); for(int i=res.size()-1;i>=0;i--) printf("%d",res[i]); printf("\n%d",t); return 0; }