C++ 高精度算法及N的阶乘

时间:2021-05-28 03:37:28
所谓高精度就是用普通类型计算都会溢出的大数运算
高精度算法在做题时经常遇到且经常性的模板化,这里做一下总结

以下的程序重载了高精度中可能遇到的多种运算符,但不能出现负数

#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 = (char)(s[i] + '0') + res;
if(res == "") res = "0";
return res;
}
//重载+
bign operator + (const bign& b) const
{
bign c;
c.len = 0;
for(int i = 0, g = 0; g || i < max(len, b.len); i++) {
int x = g;
if(i < len) x += s[i];
if(i < b.len) x += b.s[i];
c.s[c.len++] = x % 10;
g = x / 10;
}
return c;
}
//去除数据的前导0
void clean()
{
while(len > 1 && !s[len-1]) len--;
}
//重载*
bign operator * (const bign& b)
{
bign c; c.len = len + b.len;
for(int i = 0; i < len; i++)
for(int j = 0; j < b.len; j++)
c.s[i+j] += s[i] * b.s[j];
for(int i = 0; i < c.len-1; i++)
{
c.s[i+1] += c.s[i] / 10;
c.s[i] %= 10;
}
c.clean();
return c;
}
//重载-,不支持负数
bign operator - (const bign& b)
{
bign c; c.len = 0;
for(int i = 0, g = 0; i < len; i++)
{
int x = s[i] - g;
if(i < b.len) x -= b.s[i];
if(x >= 0) g = 0;
else
{
g = 1;
x += 10;
}
c.s[c.len++] = x;
}
c.clean();
return c;
}
//重载比较运算符
bool operator < (const bign& b) const
{
if(len != b.len) return len < b.len;
for(int i = len-1; i >= 0; i--)
if(s[i] != b.s[i]) return s[i] < b.s[i];
return false;
}

bool operator > (const bign& b) const
{
return b < *this;
}

bool operator <= (const bign& b)
{
return !(b > *this);
}

bool operator == (const bign& b)
{
return !(b < *this) && !(*this < b);
}

bign operator += (const bign& b)
{
*this = *this + b;
return *this;
}
};
//重载输入流
istream& operator >> (istream &in, bign& x)
{
string s;
in >> s;
x = s.c_str();
return in;
}
//重载输出流
ostream& operator << (ostream &out, const bign& x)
{
out << x.str();
return out;
}

int main()
{
bign a;
while(cin>>a>>b)
{
cout<<a.len<<endl;
cout<<a<<endl;
cout<<a+a<<endl;
cout<<a*a<<endl;
}
return 0;
}


下面是计算N的阶乘的程序的简单写法,可以根据个人情况加入上面程序的构造函数

注意:2e5的阶乘将会超过50000位,maxn根据自己情况修改大小

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=50000;
struct node
{
int f[maxn],len;
node()
{
memset(f,0,sizeof(f));
len=1;
}
node operator=(const string&p)
{
len=p.size();
for(int i=0;i<len;i++)
f[len-i-1]=p[i]-'0';
return *this;
}
node operator=(int n)
{
if(n==0)return*this;
int i=0;
while(n)
{
f[i++]=n%10;
n/=10;
}
len=i;
return*this;
}
node operator*(const node&p)
{
node tmp;
int y=0;
tmp.len=len+p.len;
for(int i=0;i<len;i++)
for(int j=0;j<p.len;j++)
tmp.f[i+j]+=f[i]*p.f[j];
for(int i=0;i<tmp.len;i++)
{
tmp.f[i+1]+=tmp.f[i]/10;
tmp.f[i]%=10;
}
if(!tmp.f[tmp.len-1])tmp.len--;
return tmp;
}
};
istream&operator>>(istream&in,node&p)
{
string s;
in>>s;
p=s;
return in;
}
ostream&operator<<(ostream&out,const node&p)
{
for(int i=p.len-1;i>=0;i--)
out<<p.f[i];
return out;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
while(cin>>n)
{
node a,t;
a=1;
for(int i=1;i<=n;i++)
{
t=i;
a=a*t;
}
cout<<a.len<<endl;
cout<<a<<endl;
}
return 0;
}