C++大數(shù)模板(推薦)
更新時(shí)間:2013年05月24日 17:03:58 作者:
本篇文章是對C++大數(shù)模板的程序代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
分別使用C++中的運(yùn)算符重載的方法來實(shí)現(xiàn)大數(shù)之間的數(shù)學(xué)運(yùn)算,包括加法、減法、乘法、除法、n次方、取模、大小比較、賦值以及輸入流、輸出流的重載。。
并且使用這個(gè)大數(shù)模板,順利AC了HDOJ上的1134這個(gè)題目的Catalan數(shù)計(jì)數(shù)問題。。
http://acm.hdu.edu.cn/showproblem.php?pid=1134
大數(shù)模板的代碼如下:
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
#define MAXN 9999
#define MAXSIZE 10
#define DLEN 4
class BigNum
{
private:
int a[500]; //可以控制大數(shù)的位數(shù)
int len; //大數(shù)長度
public:
BigNum(){ len = 1;memset(a,0,sizeof(a)); } //構(gòu)造函數(shù)
BigNum(const int); //將一個(gè)int類型的變量轉(zhuǎn)化為大數(shù)
BigNum(const char*); //將一個(gè)字符串類型的變量轉(zhuǎn)化為大數(shù)
BigNum(const BigNum &); //拷貝構(gòu)造函數(shù)
BigNum &operator=(const BigNum &); //重載賦值運(yùn)算符,大數(shù)之間進(jìn)行賦值運(yùn)算
friend istream& operator>>(istream&, BigNum&); //重載輸入運(yùn)算符
friend ostream& operator<<(ostream&, BigNum&); //重載輸出運(yùn)算符
BigNum operator+(const BigNum &) const; //重載加法運(yùn)算符,兩個(gè)大數(shù)之間的相加運(yùn)算
BigNum operator-(const BigNum &) const; //重載減法運(yùn)算符,兩個(gè)大數(shù)之間的相減運(yùn)算
BigNum operator*(const BigNum &) const; //重載乘法運(yùn)算符,兩個(gè)大數(shù)之間的相乘運(yùn)算
BigNum operator/(const int &) const; //重載除法運(yùn)算符,大數(shù)對一個(gè)整數(shù)進(jìn)行相除運(yùn)算
BigNum operator^(const int &) const; //大數(shù)的n次方運(yùn)算
int operator%(const int &) const; //大數(shù)對一個(gè)int類型的變量進(jìn)行取模運(yùn)算
bool operator>(const BigNum & T)const; //大數(shù)和另一個(gè)大數(shù)的大小比較
bool operator>(const int & t)const; //大數(shù)和一個(gè)int類型的變量的大小比較
void print(); //輸出大數(shù)
};
BigNum::BigNum(const int b) //將一個(gè)int類型的變量轉(zhuǎn)化為大數(shù)
{
int c,d = b;
len = 0;
memset(a,0,sizeof(a));
while(d > MAXN)
{
c = d - (d / (MAXN + 1)) * (MAXN + 1);
d = d / (MAXN + 1);
a[len++] = c;
}
a[len++] = d;
}
BigNum::BigNum(const char*s) //將一個(gè)字符串類型的變量轉(zhuǎn)化為大數(shù)
{
int t,k,index,l,i;
memset(a,0,sizeof(a));
l=strlen(s);
len=l/DLEN;
if(l%DLEN)
len++;
index=0;
for(i=l-1;i>=0;i-=DLEN)
{
t=0;
k=i-DLEN+1;
if(k<0)
k=0;
for(int j=k;j<=i;j++)
t=t*10+s[j]-'0';
a[index++]=t;
}
}
BigNum::BigNum(const BigNum & T) : len(T.len) //拷貝構(gòu)造函數(shù)
{
int i;
memset(a,0,sizeof(a));
for(i = 0 ; i < len ; i++)
a[i] = T.a[i];
}
BigNum & BigNum::operator=(const BigNum & n) //重載賦值運(yùn)算符,大數(shù)之間進(jìn)行賦值運(yùn)算
{
int i;
len = n.len;
memset(a,0,sizeof(a));
for(i = 0 ; i < len ; i++)
a[i] = n.a[i];
return *this;
}
istream& operator>>(istream & in, BigNum & b) //重載輸入運(yùn)算符
{
char ch[MAXSIZE*4];
int i = -1;
in>>ch;
int l=strlen(ch);
int count=0,sum=0;
for(i=l-1;i>=0;)
{
sum = 0;
int t=1;
for(int j=0;j<4&&i>=0;j++,i--,t*=10)
{
sum+=(ch[i]-'0')*t;
}
b.a[count]=sum;
count++;
}
b.len =count++;
return in;
}
ostream& operator<<(ostream& out, BigNum& b) //重載輸出運(yùn)算符
{
int i;
cout << b.a[b.len - 1];
for(i = b.len - 2 ; i >= 0 ; i--)
{
cout.width(DLEN);
cout.fill('0');
cout << b.a[i];
}
return out;
}
BigNum BigNum::operator+(const BigNum & T) const //兩個(gè)大數(shù)之間的相加運(yùn)算
{
BigNum t(*this);
int i,big; //位數(shù)
big = T.len > len ? T.len : len;
for(i = 0 ; i < big ; i++)
{
t.a[i] +=T.a[i];
if(t.a[i] > MAXN)
{
t.a[i + 1]++;
t.a[i] -=MAXN+1;
}
}
if(t.a[big] != 0)
t.len = big + 1;
else
t.len = big;
return t;
}
BigNum BigNum::operator-(const BigNum & T) const //兩個(gè)大數(shù)之間的相減運(yùn)算
{
int i,j,big;
bool flag;
BigNum t1,t2;
if(*this>T)
{
t1=*this;
t2=T;
flag=0;
}
else
{
t1=T;
t2=*this;
flag=1;
}
big=t1.len;
for(i = 0 ; i < big ; i++)
{
if(t1.a[i] < t2.a[i])
{
j = i + 1;
while(t1.a[j] == 0)
j++;
t1.a[j--]--;
while(j > i)
t1.a[j--] += MAXN;
t1.a[i] += MAXN + 1 - t2.a[i];
}
else
t1.a[i] -= t2.a[i];
}
t1.len = big;
while(t1.a[len - 1] == 0 && t1.len > 1)
{
t1.len--;
big--;
}
if(flag)
t1.a[big-1]=0-t1.a[big-1];
return t1;
}
BigNum BigNum::operator*(const BigNum & T) const //兩個(gè)大數(shù)之間的相乘運(yùn)算
{
BigNum ret;
int i,j,up;
int temp,temp1;
for(i = 0 ; i < len ; i++)
{
up = 0;
for(j = 0 ; j < T.len ; j++)
{
temp = a[i] * T.a[j] + ret.a[i + j] + up;
if(temp > MAXN)
{
temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);
up = temp / (MAXN + 1);
ret.a[i + j] = temp1;
}
else
{
up = 0;
ret.a[i + j] = temp;
}
}
if(up != 0)
ret.a[i + j] = up;
}
ret.len = i + j;
while(ret.a[ret.len - 1] == 0 && ret.len > 1)
ret.len--;
return ret;
}
BigNum BigNum::operator/(const int & b) const //大數(shù)對一個(gè)整數(shù)進(jìn)行相除運(yùn)算
{
BigNum ret;
int i,down = 0;
for(i = len - 1 ; i >= 0 ; i--)
{
ret.a[i] = (a[i] + down * (MAXN + 1)) / b;
down = a[i] + down * (MAXN + 1) - ret.a[i] * b;
}
ret.len = len;
while(ret.a[ret.len - 1] == 0 && ret.len > 1)
ret.len--;
return ret;
}
int BigNum::operator %(const int & b) const //大數(shù)對一個(gè)int類型的變量進(jìn)行取模運(yùn)算
{
int i,d=0;
for (i = len-1; i>=0; i--)
{
d = ((d * (MAXN+1))% b + a[i])% b;
}
return d;
}
BigNum BigNum::operator^(const int & n) const //大數(shù)的n次方運(yùn)算
{
BigNum t,ret(1);
int i;
if(n<0)
exit(-1);
if(n==0)
return 1;
if(n==1)
return *this;
int m=n;
while(m>1)
{
t=*this;
for( i=1;i<<1<=m;i<<=1)
{
t=t*t;
}
m-=i;
ret=ret*t;
if(m==1)
ret=ret*(*this);
}
return ret;
}
bool BigNum::operator>(const BigNum & T) const //大數(shù)和另一個(gè)大數(shù)的大小比較
{
int ln;
if(len > T.len)
return true;
else if(len == T.len)
{
ln = len - 1;
while(a[ln] == T.a[ln] && ln >= 0)
ln--;
if(ln >= 0 && a[ln] > T.a[ln])
return true;
else
return false;
}
else
return false;
}
bool BigNum::operator >(const int & t) const //大數(shù)和一個(gè)int類型的變量的大小比較
{
BigNum b(t);
return *this>b;
}
void BigNum::print() //輸出大數(shù)
{
int i;
cout << a[len - 1];
for(i = len - 2 ; i >= 0 ; i--)
{
cout.width(DLEN);
cout.fill('0');
cout << a[i];
}
cout << endl;
}
int main(void)
{
int i,n;
BigNum x[101]; //定義大數(shù)的對象數(shù)組
x[0]=1;
for(i=1;i<101;i++)
x[i]=x[i-1]*(4*i-2)/(i+1);
while(scanf("%d",&n)==1 && n!=-1)
{
x[n].print();
}
}
并且使用這個(gè)大數(shù)模板,順利AC了HDOJ上的1134這個(gè)題目的Catalan數(shù)計(jì)數(shù)問題。。
http://acm.hdu.edu.cn/showproblem.php?pid=1134
大數(shù)模板的代碼如下:
復(fù)制代碼 代碼如下:
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
#define MAXN 9999
#define MAXSIZE 10
#define DLEN 4
class BigNum
{
private:
int a[500]; //可以控制大數(shù)的位數(shù)
int len; //大數(shù)長度
public:
BigNum(){ len = 1;memset(a,0,sizeof(a)); } //構(gòu)造函數(shù)
BigNum(const int); //將一個(gè)int類型的變量轉(zhuǎn)化為大數(shù)
BigNum(const char*); //將一個(gè)字符串類型的變量轉(zhuǎn)化為大數(shù)
BigNum(const BigNum &); //拷貝構(gòu)造函數(shù)
BigNum &operator=(const BigNum &); //重載賦值運(yùn)算符,大數(shù)之間進(jìn)行賦值運(yùn)算
friend istream& operator>>(istream&, BigNum&); //重載輸入運(yùn)算符
friend ostream& operator<<(ostream&, BigNum&); //重載輸出運(yùn)算符
BigNum operator+(const BigNum &) const; //重載加法運(yùn)算符,兩個(gè)大數(shù)之間的相加運(yùn)算
BigNum operator-(const BigNum &) const; //重載減法運(yùn)算符,兩個(gè)大數(shù)之間的相減運(yùn)算
BigNum operator*(const BigNum &) const; //重載乘法運(yùn)算符,兩個(gè)大數(shù)之間的相乘運(yùn)算
BigNum operator/(const int &) const; //重載除法運(yùn)算符,大數(shù)對一個(gè)整數(shù)進(jìn)行相除運(yùn)算
BigNum operator^(const int &) const; //大數(shù)的n次方運(yùn)算
int operator%(const int &) const; //大數(shù)對一個(gè)int類型的變量進(jìn)行取模運(yùn)算
bool operator>(const BigNum & T)const; //大數(shù)和另一個(gè)大數(shù)的大小比較
bool operator>(const int & t)const; //大數(shù)和一個(gè)int類型的變量的大小比較
void print(); //輸出大數(shù)
};
BigNum::BigNum(const int b) //將一個(gè)int類型的變量轉(zhuǎn)化為大數(shù)
{
int c,d = b;
len = 0;
memset(a,0,sizeof(a));
while(d > MAXN)
{
c = d - (d / (MAXN + 1)) * (MAXN + 1);
d = d / (MAXN + 1);
a[len++] = c;
}
a[len++] = d;
}
BigNum::BigNum(const char*s) //將一個(gè)字符串類型的變量轉(zhuǎn)化為大數(shù)
{
int t,k,index,l,i;
memset(a,0,sizeof(a));
l=strlen(s);
len=l/DLEN;
if(l%DLEN)
len++;
index=0;
for(i=l-1;i>=0;i-=DLEN)
{
t=0;
k=i-DLEN+1;
if(k<0)
k=0;
for(int j=k;j<=i;j++)
t=t*10+s[j]-'0';
a[index++]=t;
}
}
BigNum::BigNum(const BigNum & T) : len(T.len) //拷貝構(gòu)造函數(shù)
{
int i;
memset(a,0,sizeof(a));
for(i = 0 ; i < len ; i++)
a[i] = T.a[i];
}
BigNum & BigNum::operator=(const BigNum & n) //重載賦值運(yùn)算符,大數(shù)之間進(jìn)行賦值運(yùn)算
{
int i;
len = n.len;
memset(a,0,sizeof(a));
for(i = 0 ; i < len ; i++)
a[i] = n.a[i];
return *this;
}
istream& operator>>(istream & in, BigNum & b) //重載輸入運(yùn)算符
{
char ch[MAXSIZE*4];
int i = -1;
in>>ch;
int l=strlen(ch);
int count=0,sum=0;
for(i=l-1;i>=0;)
{
sum = 0;
int t=1;
for(int j=0;j<4&&i>=0;j++,i--,t*=10)
{
sum+=(ch[i]-'0')*t;
}
b.a[count]=sum;
count++;
}
b.len =count++;
return in;
}
ostream& operator<<(ostream& out, BigNum& b) //重載輸出運(yùn)算符
{
int i;
cout << b.a[b.len - 1];
for(i = b.len - 2 ; i >= 0 ; i--)
{
cout.width(DLEN);
cout.fill('0');
cout << b.a[i];
}
return out;
}
BigNum BigNum::operator+(const BigNum & T) const //兩個(gè)大數(shù)之間的相加運(yùn)算
{
BigNum t(*this);
int i,big; //位數(shù)
big = T.len > len ? T.len : len;
for(i = 0 ; i < big ; i++)
{
t.a[i] +=T.a[i];
if(t.a[i] > MAXN)
{
t.a[i + 1]++;
t.a[i] -=MAXN+1;
}
}
if(t.a[big] != 0)
t.len = big + 1;
else
t.len = big;
return t;
}
BigNum BigNum::operator-(const BigNum & T) const //兩個(gè)大數(shù)之間的相減運(yùn)算
{
int i,j,big;
bool flag;
BigNum t1,t2;
if(*this>T)
{
t1=*this;
t2=T;
flag=0;
}
else
{
t1=T;
t2=*this;
flag=1;
}
big=t1.len;
for(i = 0 ; i < big ; i++)
{
if(t1.a[i] < t2.a[i])
{
j = i + 1;
while(t1.a[j] == 0)
j++;
t1.a[j--]--;
while(j > i)
t1.a[j--] += MAXN;
t1.a[i] += MAXN + 1 - t2.a[i];
}
else
t1.a[i] -= t2.a[i];
}
t1.len = big;
while(t1.a[len - 1] == 0 && t1.len > 1)
{
t1.len--;
big--;
}
if(flag)
t1.a[big-1]=0-t1.a[big-1];
return t1;
}
BigNum BigNum::operator*(const BigNum & T) const //兩個(gè)大數(shù)之間的相乘運(yùn)算
{
BigNum ret;
int i,j,up;
int temp,temp1;
for(i = 0 ; i < len ; i++)
{
up = 0;
for(j = 0 ; j < T.len ; j++)
{
temp = a[i] * T.a[j] + ret.a[i + j] + up;
if(temp > MAXN)
{
temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);
up = temp / (MAXN + 1);
ret.a[i + j] = temp1;
}
else
{
up = 0;
ret.a[i + j] = temp;
}
}
if(up != 0)
ret.a[i + j] = up;
}
ret.len = i + j;
while(ret.a[ret.len - 1] == 0 && ret.len > 1)
ret.len--;
return ret;
}
BigNum BigNum::operator/(const int & b) const //大數(shù)對一個(gè)整數(shù)進(jìn)行相除運(yùn)算
{
BigNum ret;
int i,down = 0;
for(i = len - 1 ; i >= 0 ; i--)
{
ret.a[i] = (a[i] + down * (MAXN + 1)) / b;
down = a[i] + down * (MAXN + 1) - ret.a[i] * b;
}
ret.len = len;
while(ret.a[ret.len - 1] == 0 && ret.len > 1)
ret.len--;
return ret;
}
int BigNum::operator %(const int & b) const //大數(shù)對一個(gè)int類型的變量進(jìn)行取模運(yùn)算
{
int i,d=0;
for (i = len-1; i>=0; i--)
{
d = ((d * (MAXN+1))% b + a[i])% b;
}
return d;
}
BigNum BigNum::operator^(const int & n) const //大數(shù)的n次方運(yùn)算
{
BigNum t,ret(1);
int i;
if(n<0)
exit(-1);
if(n==0)
return 1;
if(n==1)
return *this;
int m=n;
while(m>1)
{
t=*this;
for( i=1;i<<1<=m;i<<=1)
{
t=t*t;
}
m-=i;
ret=ret*t;
if(m==1)
ret=ret*(*this);
}
return ret;
}
bool BigNum::operator>(const BigNum & T) const //大數(shù)和另一個(gè)大數(shù)的大小比較
{
int ln;
if(len > T.len)
return true;
else if(len == T.len)
{
ln = len - 1;
while(a[ln] == T.a[ln] && ln >= 0)
ln--;
if(ln >= 0 && a[ln] > T.a[ln])
return true;
else
return false;
}
else
return false;
}
bool BigNum::operator >(const int & t) const //大數(shù)和一個(gè)int類型的變量的大小比較
{
BigNum b(t);
return *this>b;
}
void BigNum::print() //輸出大數(shù)
{
int i;
cout << a[len - 1];
for(i = len - 2 ; i >= 0 ; i--)
{
cout.width(DLEN);
cout.fill('0');
cout << a[i];
}
cout << endl;
}
int main(void)
{
int i,n;
BigNum x[101]; //定義大數(shù)的對象數(shù)組
x[0]=1;
for(i=1;i<101;i++)
x[i]=x[i-1]*(4*i-2)/(i+1);
while(scanf("%d",&n)==1 && n!=-1)
{
x[n].print();
}
}
您可能感興趣的文章:
- 詳解C++的模板中typename關(guān)鍵字的用法
- C++關(guān)鍵字typename的深入理解
- c++中typename和class的區(qū)別介紹
- C++中函數(shù)模板的用法詳細(xì)解析
- C++模板之特化與偏特化詳解
- C++中的類模板詳解及示例
- C++類模板與模板類深入詳解
- C++函數(shù)模板與類模板實(shí)例解析
- C++標(biāo)準(zhǔn)模板庫函數(shù)sort的那些事兒
- 淺析C++中模板的那點(diǎn)事
- C++模板特例化應(yīng)用實(shí)例
- C++可變參數(shù)的函數(shù)與模板實(shí)例分析
- 深入分析:C++模板究竟會(huì)使代碼膨脹嗎
- C++的template模板中class與typename關(guān)鍵字的區(qū)別分析
相關(guān)文章
C++實(shí)現(xiàn)企業(yè)職工工資管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)企業(yè)職工工資管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01C語言詳細(xì)圖解浮點(diǎn)型數(shù)據(jù)的存儲(chǔ)實(shí)現(xiàn)
使用編程語言進(jìn)行編程時(shí),需要用到各種變量來存儲(chǔ)各種信息。變量保留的是它所存儲(chǔ)的值的內(nèi)存位置。這意味著,當(dāng)您創(chuàng)建一個(gè)變量時(shí),就會(huì)在內(nèi)存中保留一些空間。您可能需要存儲(chǔ)各種數(shù)據(jù)類型的信息,操作系統(tǒng)會(huì)根據(jù)變量的數(shù)據(jù)類型,來分配內(nèi)存和決定在保留內(nèi)存中存儲(chǔ)什么2022-05-05C語言實(shí)現(xiàn)簡易學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡易學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06C語言鏈表實(shí)現(xiàn)銷售管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言鏈表實(shí)現(xiàn)銷售管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02c++指針參數(shù)傳遞和引用參數(shù)傳遞的區(qū)別解析
這篇文章主要介紹了c++指針參數(shù)傳遞和引用參數(shù)傳遞的區(qū)別解析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07C語言獲取Linux系統(tǒng)精確時(shí)間的方法
下面小編就為大家?guī)硪黄狢語言獲取Linux系統(tǒng)精確時(shí)間的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09C語言?八大排序算法的過程圖解及實(shí)現(xiàn)代碼
排序是數(shù)據(jù)結(jié)構(gòu)中很重要的一章,本文主要為大家介紹了常用的八個(gè)排序算法(插入,希爾,選擇,堆排,冒泡,快排,歸并,計(jì)數(shù))的過程及代碼實(shí)現(xiàn),需要的朋友可以參考一下2021-12-12