HDU1042 N!
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042
#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;
}
BigInteger BigInteger::operator+(const BigInteger & n) const
{
BigInteger a = *this;
a.add(n);
return a;
}
BigInteger BigInteger::operator-(const BigInteger & T) const
{
BigInteger b = *this;
b.sub(T);
return b;
}
BigInteger BigInteger::operator * (const BigInteger &rhs) const
{
BigInteger result;
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;
}
BigInteger BigInteger::operator/(const int & b) const
{
BigInteger ret;
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;
BigInteger result,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;
}
#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;
}
BigInteger BigInteger::operator+(const BigInteger & n) const
{
BigInteger a = *this;
a.add(n);
return a;
}
BigInteger BigInteger::operator-(const BigInteger & T) const
{
BigInteger b = *this;
b.sub(T);
return b;
}
BigInteger BigInteger::operator * (const BigInteger &rhs) const
{
BigInteger result;
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;
}
BigInteger BigInteger::operator/(const int & b) const
{
BigInteger ret;
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;
BigInteger result,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;
}
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。
posted on 2009-09-14 16:23 Phinecos(洞庭散人) 阅读(1459) 评论(2) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述