HDU1042 N!

题目链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=1042

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> #include < iostream >
#include
< stdio.h >
#include
< string >
#include
< iomanip >
#include
< algorithm >
using namespace std;

const int MAX_GROUPS = 10000 ; // 最多万组,每组最多位整数,即最多可容纳万位整数
const int MAXN = 9999 ; // 每组的上限值
const int GROUP_LEN = 4 ; // 每组的最大长度

class BigInteger
{
private :
int data[MAX_GROUPS];
int len;
void init()
{
memset(data,
0 , sizeof (data));
}
public :
BigInteger()
{
init();
len
= 0 ;
}
BigInteger(
const int b);
BigInteger(
const BigInteger & );

bool operator > ( const BigInteger & ) const ;
BigInteger
& operator = ( const BigInteger & );
BigInteger
& add( const BigInteger & );
BigInteger
& sub( const BigInteger & );
BigInteger
operator + ( const BigInteger & ) const ;
BigInteger
operator - ( const BigInteger & ) const ;
BigInteger
operator * ( const BigInteger & ) const ;
BigInteger
operator / ( const int & ) const ;
void print();
};
BigInteger::BigInteger(
const int num)
{
int res,tmp = num;
len
= 0 ;
init();
while (tmp > MAXN)
{
res
= tmp - tmp / (MAXN + 1 ) * (MAXN + 1 );
tmp
= tmp / (MAXN + 1 );
data[len
++ ] = res;
}
data[len
++ ] = tmp;
}
BigInteger::BigInteger(
const BigInteger & rhs):len(rhs.len)
{
int i;
init();
for (i = 0 ;i < len;i ++ )
{
data[i]
= rhs.data[i];
}
}
bool BigInteger:: operator > ( const BigInteger & rhs) const
{
int ln;
if (len > rhs.len)
{
return true ;
}
else if (len < rhs.len)
{
return false ;
}
else if (len == rhs.len)
{
ln
= len - 1 ;
while (data[ln] == rhs.data[ln] && ln >= 0 )
{
ln
-- ;
}
if (ln >= 0 && data[ln] > rhs.data[ln])
{
return true ;
}
else
{
return false ;
}
}

}

BigInteger
& BigInteger:: operator = ( const BigInteger & rhs)
{
init();
len
= rhs.len;
for ( int i = 0 ;i < len;i ++ )
{
data[i]
= rhs.data[i];
}
return * this ;
}
BigInteger
& BigInteger::add( const BigInteger & rhs)
{
int i,nLen;

nLen
= rhs.len > len ? rhs.len:len;
for (i = 0 ;i < nLen;i ++ )
{
data[i]
= data[i] + rhs.data[i];
if (data[i] > MAXN)
{
data[i
+ 1 ] ++ ;
data[i]
= data[i] - MAXN - 1 ;
}
}
if (data[nLen] != 0 )
{
len
= nLen + 1 ;
}
else
{
len
= nLen;
}

return * this ;
}
BigInteger
& BigInteger::sub( const BigInteger & rhs)
{
int i,j,nLen;
if (len > rhs.len)
{
for (i = 0 ;i < nLen;i ++ )
{
if (data[i] < rhs.data[i])
{
j
= i + 1 ;
while (data[j] == 0 )j ++ ;
data[j]
-- ;
-- j;
while (j > i)
{
data[j]
+= MAXN;
-- j;
}
data[i]
= data[i] + MAXN + 1 - rhs.data[i];
}
else
{
data[i]
-= rhs.data[i];
}
}
len
= nLen;
while (data[len - 1 ] == 0 && len > 1 )
{
-- len;
}
}
else if (len == rhs.len)
{
for (i = 0 ;i < len;i ++ )
{
data[i]
-= rhs.data[i];
}
while (data[len - 1 ] == 0 && len > 1 )
{
-- len;
}
}
return * this ;
}
BigIntegerBigInteger::
operator + ( const BigInteger & n) const
{
BigIntegera
= * this ;
a.add(n);
return a;
}
BigIntegerBigInteger::
operator - ( const BigInteger & T) const
{
BigIntegerb
= * this ;
b.sub(T);
return b;
}
BigIntegerBigInteger::
operator * ( const BigInteger & rhs) const
{
BigIntegerresult;
int i,j,up;
int temp,temp1;

for (i = 0 ;i < len;i ++ )
{
up
= 0 ;
for (j = 0 ;j < rhs.len;j ++ )
{
temp
= data[i] * rhs.data[j] + result.data[i + j] + up;
if (temp > MAXN)
{
temp1
= temp - temp / (MAXN + 1 ) * (MAXN + 1 );
up
= temp / (MAXN + 1 );
result.data[i
+ j] = temp1;
}
else
{
up
= 0 ;
result.data[i
+ j] = temp;
}
}
if (up != 0 )
{
result.data[i
+ j] = up;
}
}
result.len
= i + j;
while (result.data[result.len - 1 ] == 0 && result.len > 1 )result.len -- ;
return result;
}
BigIntegerBigInteger::
operator / ( const int & b) const
{
BigIntegerret;
int i,down = 0 ;

for (i = len - 1 ;i >= 0 ;i -- )
{
ret.data[i]
= (data[i] + down * (MAXN + 1 )) / b;
down
= data[i] + down * (MAXN + 1 ) - ret.data[i] * b;
}
ret.len
= len;
while (ret.data[ret.len - 1 ] == 0 )ret.len -- ;
return ret;
}
void BigInteger::print()
{
int i;

cout
<< data[len - 1 ];
for (i = len - 2 ;i >= 0 ;i -- )
{
cout.width(GROUP_LEN);
cout.fill(
' 0 ' );
cout
<< data[i];
}
cout
<< endl;
}
int main()
{
int i,n;
BigIntegerresult,num;

while (scanf( " %d " , & n) != EOF)
{
result
= BigInteger( 1 );
for (i = 2 ;i <= n; ++ i)
{
num
= BigInteger(i);
result
= result * num;
}
result.print();
}
return 0 ;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值