高精度加法
题目链接:高精度加法
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5+5;
int a[N],b[N];
int c[N];
int n,m;
string s1,s2;signed main(){ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);cin>>s1>>s2;int n=s1.length();int m=s2.length();for(int i=1;i<=n;i++){a[n-i+1]+=s1[i-1]-'0';}for(int i=1;i<=m;i++){b[m-i+1]+=s2[i-1]-'0';}int jw=0;for(int i=1;i<=max(n,m);i++){c[i]=a[i]+b[i]+jw;jw=c[i]/10;c[i]%=10;}if(jw){c[max(n,m)+1]=jw;}int k=max(n,m)+1;while(c[k]==0)k--;for(int i=k;i>=1;i--){cout<<c[i];}return 0;
}
高精度减法
题目链接:高精度减法
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N =1e5+5;
int a[N],b[N],c[N];
string s1,s2;
bool flag=true;signed main(){ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);cin>>s1>>s2;int n=s1.length();int m=s2.length();if(n<m){flag=false;}else if(n==m){int l=0;while(s1[l]==s2[l]&&l<=n)l++;if(s1[l]<s2[l]){flag=false;}}int cnt1=0,cnt2=0;if(flag){for(int i=1;i<=n;i++){a[++cnt1]=s1[i-1]-'0';}for(int i=1;i<=m;i++){b[++cnt2]=s2[i-1]-'0';}}else{for(int i=1;i<=m;i++){a[++cnt1]=s2[i-1]-'0';}for(int i=1;i<=n;i++){b[++cnt2]=s1[i-1]-'0';}}reverse(a+1,a+1+cnt1);reverse(b+1,b+1+cnt2);int jw=0;for(int i=1;i<=max(n,m);i++){c[i]=a[i]-b[i]-jw;if(c[i]<0)jw=1;else jw=0;c[i]=(c[i]+10)%10;}int k=max(n,m)+1;while(c[k]==0&&k>1)k--;//注意k要大于1if(!flag)cout<<'-';for(int i=k;i>=1;i--){cout<<c[i];}return 0;
}
高精度乘法
题目链接:高精度乘法
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5+5;
int a[N],b[N],c[N];
string s1,s2;signed main(){ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);cin>>s1>>s2;int n=s1.length();int m=s2.length();for(int i=1;i<=n;i++){a[n-i+1]=s1[i-1]-'0';}for(int i=1;i<=m;i++){b[m-i+1]=s2[i-1]-'0';}int jw=0;for(int i=1;i<=n;i++){jw=0;for(int j=1;j<=m;j++){c[i+j-1]+=a[i]*b[j]+jw;jw=c[i+j-1]/10;c[i+j-1]%=10;}c[i+m]+=jw;}int k=n+m+1;while(c[k]==0&&k>1)k--;for(int i=k;i>=1;i--){cout<<c[i];}return 0;
}
高精度除法
题目链接:高精度除法
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5+5;
int a[N],c[N];
string s1;
int b;signed main(){ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);cin>>s1>>b;int n=s1.length();for(int i=1;i<=n;i++){a[i]=s1[i-1]-'0';}int x=0;for(int i=1;i<=n;i++){c[i]=(x*10+a[i])/b;x=(x*10+a[i])%b;}int sb=1;while(c[sb]==0&&sb<n){sb++;}for(int i=sb;i<=n;i++){cout<<c[i];}cout<<"\n";cout<<x;return 0;
}