C++大數(shù)模板(推薦)
分別使用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ù)長(zhǎng)度
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ù)對(duì)一個(gè)整數(shù)進(jìn)行相除運(yùn)算
BigNum operator^(const int &) const; //大數(shù)的n次方運(yùn)算
int operator%(const int &) const; //大數(shù)對(duì)一個(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ù)對(duì)一個(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ù)對(duì)一個(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ù)的對(duì)象數(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語言
下一篇:深入理解atoi()與itoa()函數(shù)的用法
本文標(biāo)題:C++大數(shù)模板(推薦)
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/4517.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10深入第K大數(shù)問題以及算法概要的詳解
- 01-10c++中inline的用法分析
- 01-10如何尋找數(shù)組中的第二大數(shù)
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10大數(shù)(高精度數(shù))模板(分享)
- 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法