目录
高精度加法
高精度减法
高精度乘法
高精度除法
高精度加法:
1,把数当做字符串处理
2, 大于10进位
3,反转一下可能更好处理
#include <iostream>
#include <string>
#include <algorithm>
const int N = 1e6;
int v[N];
int a[N];
int b[N];
using namespace std;
int main()
{
string A, B;
cin >> A >> B;
reverse(A.begin(),A.end());
reverse(B.begin(),B.end());
string C;
int i,kk,k;
for (kk=0 ; kk<A.size(); kk++ )
a[kk]+=(int)(A[kk]-'0');
for (k=0 ; k<B.size(); k++ )
b[k]+=(int)(B[k]-'0');
for(i;i<kk||i<k;i++)
{
v[i]+=(a[i]+b[i]);
if(v[i]>=10)
{
v[i]-=10;
v[i+1]++;
}
}
int flag=0;
for(i=kk+k+1;i>=0;i--)
{
if(v[i])
flag++;
if(flag)
C+=('0'+v[i]);
}
if(flag==0)
C+=('0');
for(int i=0;i<C.size();i++)
cout<<C[i];
return 0;
}
高精度减法
1,让大数减去小数
2,使用vector
3,压位
#include<bits/stdc++.h>
using namespace std;
vector<int> res,x,y;
bool vector_swap(vector<int> x,vector<int> y)
{
if(x.size()>y.size())return true;
if(x.size()<y.size())return false;
for(int i=0;i<x.size();i++)
{
if(x[i]>y[i])return true;
if(x[i]<y[i])return false;
}
return true;
}
vector<int> minuse(vector<int> x,vector<int> y)
{
int t=0;
reverse(x.begin(),x.end());
reverse(y.begin(),y.end());
for(int j=0;j<x.size();j++)
{
int s=0;
if(j<y.size())s-=y[j];
s+=(x[j]+t);
if(s>=0)res.push_back(s),t=0;
else res.push_back(s+10),t=-1;
}
int i=res.size()-1;
while(i>0&&res[i]==0)i--,res.pop_back();
return res;
}
int main()
{
string a,b;
cin>>a>>b;
for(int i=0;i<a.size();i++)x.push_back(a[i]-'0');
for(int i=0;i<b.size();i++)y.push_back(b[i]-'0');
if(vector_swap(x,y)==false)swap(x,y),cout<<'-';
minuse(x,y);
for(int i=res.size()-1;i>=0;i--)
cout<<res[i];
return 0;
}
#include<bits/stdc++.h>
using namespace std;
vector<int> res,x,y;
int cheak(string A,string B)
{
if(A.size()>B.size())
return 0;
if(A.size()==B.size())
return B>A;
return 1;
}
vector<int> minuse(vector<int> x,vector<int> y)
{
int t=0;
reverse(x.begin(),x.end());
reverse(y.begin(),y.end());
for(int j=0;j<x.size();j++)
{
int s=0;
if(j<y.size())s-=y[j];
s+=(x[j]+t);
if(s>=0)res.push_back(s),t=0;
else res.push_back(s+10),t=-1;
}
int i=res.size()-1;
while(i>0&&res[i]==0)i--,res.pop_back();
return res;
}
int main()
{
string a,b;
cin>>a>>b;
for(int i=0;i<a.size();i++)x.push_back(a[i]-'0');
for(int i=0;i<b.size();i++)y.push_back(b[i]-'0');
while(a.size()&&a[0]=='0')
{
a.erase(0,1);
}
cin>>b;
while(b.size()&&b[0]=='0')
{
b.erase(0,1);
}
if(cheak(a,b))
{
cout<<'-';
minuse(y,x);
}
else
minuse(x,y);
for(int i=res.size()-1;i>=0;i--)
cout<<res[i];
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
bool cmp(vector<int> &A, vector<int> &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;
for (int i = 0, t = 0; i < A.size(); i ++ )
{
t = A[i] - t;
if (i < B.size()) t -= B[i];
C.push_back((t + 10) % 10);
if (t < 0) t = 1;
else t = 0;
}
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
int main()
{
string a, b;
vector<int> A, B;
cin >> 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);
else C = sub(B, A), cout << '-';
for (int i = C.size() - 1; i >= 0; i -- ) cout << C[i];
cout << endl;
return 0;
}
作者:yxc
链接:https://www.acwing.com/activity/content/code/content/39793/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
高精度乘法
for (int i = 0; i < A.size(); i ++ )
for (int j = 0; j < B.size(); j ++ )
C[i + j] += A[i] * B[j];
//乘法
for (int i = 0, t = 0; i < C.size(); i ++ )
{
t += C[i];
C[i] = t % 10;
t /= 10;
}
//处理
高精度减法
for(int i = A.size() - 1; i >= 0; i --)
{
r = r * 10 + A[i];
C.push_back(r / b);
r %= b;
}
reverse(C.begin(), C.end());
//大数除小数
//从前往后
|