复习一下高精度~~
1.高精度加法
#include<bits/stdc++.h>
using namespace std;
const int maxn=100010;
int a[maxn],b[maxn],w[maxn];
void File_in_and_out();
void scan_num1_and_num2();
void Plus();
void print_ans();
int main()
{
//File_in_and_out();
scan_num1_and_num2();
Plus();
print_ans();
return 0;
}
void File_in_and_out()
{
freopen("plus.in","r",stdin);
freopen("plus.out","w",stdout);
}
void scan_num1_and_num2()
{
string num1,num2;
cin>>num1;
cin>>num2;
a[0]=num1.length();
b[0]=num2.length();
for(int i=0;i<=a[0]-1;i++)
{
a[a[0]-i]=num1[i]-'0';
}
for(int i=0;i<=b[0]-1;i++)
{
b[b[0]-i]=num2[i]-'0';
}
}
void Plus()
{
int ex=0;
for(int i=1;i<=a[0] || i<=b[0];i++)
{
w[i]=(a[i]+b[i]+ex)%10;
ex=(a[i]+b[i]+ex)/10;
w[0]++;
}
if(ex!=0)
{
w[++w[0]]=ex;
}
}
void print_ans()
{
for(int i=w[0];i>=1;i--)
{
printf("%d",w[i]);
}
}
对比一下Python的
a=input()
b=input()
a=int(a)
b=int(b)
print(a+b)
(Python真好用 )
2.高精度减法
#include<bits/stdc++.h>
using namespace std;
int sign=0;
string a,b;
const int maxn=100010;
int num1[maxn],num2[maxn],ans[maxn];
void scan();
void reduce();
void print();
int main()
{
scan();
reduce();
print();
return 0;
}
void scan()
{
cin>>a;
cin>>b;
num1[0]=a.length();
num2[0]=b.length();
if(num1[0]<num2[0] || (num1[0]==num2[0]&&a<b))
{
string n;
int num;
n=a;
a=b;
b=n;
num=num1[0];
num1[0]=num2[0];
num2[0]=num;
sign=-1;
}
for(int i=1;i<=num1[0];i++)
{
num1[i]=a[num1[0]-i]-'0';
//printf("%d:%d\n",i,num1[i]);
}
for(int i=1;i<=num2[0];i++)
{
num2[i]=b[num2[0]-i]-'0';
//printf("%d:%d\n",i,num2[i]);
}
}
void reduce()
{
int i=1;
while(i<=num1[0] || i<=num2[0])
{
if(num1[i]<num2[i])
{
num1[i+1]--;
num1[i]=num1[i]+10;
}
ans[i]=num1[i]-num2[i];
i++;
}
ans[0]=i;
while(ans[ans[0]]==0 && ans[0]>1)
{
ans[0]--;
}
}
void print()
{
if(sign==-1) printf("-");
for(int i=ans[0];i>=1;i--)
{
printf("%d",ans[i]);
}
}
3.高精度乘法
#include<bits/stdc++.h>
using namespace std;
const int maxn=20010;
int num1[maxn],num2[maxn],ans[maxn];
void File_Input_and_Output();
void scan();
void multiplication();
void print();
int main()
{
// File_Input_and_Output();
// int n;
// scanf("%d",&n);
// for(int i=1;i<=n;i++)
// {
memset(num1,0,sizeof(num1));
memset(num2,0,sizeof(num2));
memset(ans,0,sizeof(ans));
scan();
multiplication();
print();
// }
return 0;
}
void File_Input_and_Output()
{
freopen("Mutiplication.in","r",stdin);
freopen("Mutiplication.out","w",stdout);
}
void scan()
{
string a,b;
cin>>a;
cin>>b;
num1[0]=a.length();
num2[0]=b.length();
for(int i=0;i<=num1[0]-1;i++)
num1[num1[0]-i]=a[i]-'0';
for(int i=0;i<=num2[0]-1;i++)
num2[num2[0]-i]=b[i]-'0';
}
void multiplication()
{
for(int i=1;i<=num1[0];i++)
{
int x=0;
for(int j=1;j<=num2[0];j++)
{
ans[i+j-1]=ans[i+j-1]+x+num1[i]*num2[j];
x=ans[i+j-1]/10;
ans[i+j-1]=ans[i+j-1]%10;
}
ans[i+num2[0]]=x;
ans[0]=num1[0]+num2[0];
while(ans[ans[0]]==0 && ans[0]>1)
{
ans[0]--;
}
}
}
void print()
{
for(int i=ans[0];i>=1;i--)
{
printf("%d",ans[i]);
}
printf("\n");
}
4.高精度除法
(1).高精度除以单精度(高精度运算里最短的代码)
#include<string>
#include<iostream>
using namespace std;
string a,c;
int b,i,d;
int main()
{
cin >> a >> b;
for (;i<a.length();i++) a[i]-=48;
for (i=0;i<a.length();i++) c.push_back((d*10+a[i])/b+48),d=(d*10+a[i])%b;
for (i=0;c[0]==48;i++)c.erase(c.begin(),c.begin()+1);
cout << c;
return 0;
}
(2).高精度除以高精度
C++的还不会(学会了补上 不太可能会去学),拿Python的凑一下~~
a , b = int(input()),int(input())
print (a // b)
(Python真香)
5.重定义运算符
我写的这个只适用于输入是低精度(你也可以自己把输入的运算符也一重载),然后结果是高(低)精度的输出,多次进行高精度运算会比较方便。
(注意数据的位数,我只开了500位,而且只写了乘法和除法的 加法和减法就随缘吧)
P1045 [NOIP2003 普及组] 麦森数
P1080 [NOIP2012 提高组] 国王游戏
这两个题可以试试水
// ============================================================
// #pragma GCC optimize(2)
// #pragma GCC optimize(3,"Ofast","inline")
// ============================================================
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <utility>
using namespace std;
const int maxn=1e6+10;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-6;
//
template<class T>
inline void read(T &res)
{
char c;
T flag=1;
while((c=getchar())<'0'||c>'9') if(c=='-') flag=-1; res=c-'0';
while((c=getchar())>='0'&&c<='9') res=res*10+c-'0'; res*=flag;
}
template<class T>
void write(T res)
{
if(res<0) res=-res,putchar('-');
if(res>=10) write(res/10);
putchar('0'+res%10);
}
//
struct high_accuracy
{
int data[510];
high_accuracy()
{
memset(data,0,sizeof(data));
data[0]=1;
}
high_accuracy(int x) //这个是读入赋值
{
memset(data,0,sizeof(data));
data[0] = 1;
int digit = 1;
while(x)
{
data[digit++] = x % 10;
x /= 10;
}
data[0] = --digit;
}
high_accuracy operator *(const high_accuracy &x) //高精乘
{
high_accuracy ans;
int digit;
for(int i = 1;i <= data[0];i++)
{
int ex = 0;
for(int j = 1;j <= x.data[0];j++)
{
ans.data[i+j-1] = ans.data[i+j-1] + ex + data[i] * x.data[j];
ex = ans.data[i+j-1] / 10;
ans.data[i+j-1] = ans.data[i+j-1] % 10;
}
ans.data[i + x.data[0]] = ex;
ans.data[0] = data[0] + x.data[0];
while(ans.data[ans.data[0]]==0 && ans.data[0]>1)
{
ans.data[0]--;
}
}
return ans;
}
high_accuracy operator /(const int &x) //高精度除以低精度
{
high_accuracy ans;
ans.data[0]=data[0];
int rest = 0;
for (int i = data[0]; i >= 1; i--)
{
rest = rest * 10 + data[i];
ans.data[i] = rest / x;
rest %= x;
}
while (!ans.data[ans.data[0]] && ans.data[0]>1) ans.data[0]--;
return ans;
}
};
ostream& operator << (ostream& out,const high_accuracy &x) //这个可以直接输出ans
{
for(int i = x.data[0];i >= 1;i--) out << x.data[i] ;
return out;
}
int main()
{
clock_t c1 = clock();
/*----------------------------------------------------------*/
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//============================================================
#ifdef LOCAL
freopen("in.in","r",stdin);
freopen("out.out","w",stdout);
#endif
//============================================================
int a,b;
cin >> a >> b;
high_accuracy k(a); //这样赋值
cout << k << endl; //这样输出
end:
cerr << "Time Used:" << clock()-c1 << "ms" << endl;
return 0;
}