所谓高精度就是用普通类型计算都会溢出的大数运算
高精度算法在做题时经常遇到且经常性的模板化,这里做一下总结
高精度算法在做题时经常遇到且经常性的模板化,这里做一下总结
以下的程序重载了高精度中可能遇到的多种运算符,但不能出现负数
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
//输出数据最大长度,根据情况更改大小,不要太大
const int maxn = 50000;
struct bign
{
int len, s[maxn];
bign() //初始化构造函数
{
memset(s, 0, sizeof(s));
len = 1;
}
//构造函数
bign(int num)
{
*this = num;
}
//构造函数
bign(const char* num)
{
*this = num;
}
//重载int=号
bign operator = (int num)
{
char s[maxn];
sprintf(s, "%d", num);
*this = s;
return *this;
}
//重载字符型=
bign operator = (const char* num)
{
len = strlen(num);
for(int i = 0; i < len; i++) s[i] = num[len-i-1] - '0';
return *this;
}
//将数组s转化为字符串
string str() const
{
string res = "";
for(int i = 0; i < len; i++) res =