求N!的值

1、求N!的值
【问题描述】
用高精度方法,求N!的精确值(N以一般整数输入)。
【输入样例】ni.in
10
【输出样例】ni.out
3628800

N以整数输入,那样的话只需要生成一个sum高精变量就可以了,因为我比较弱,不太喜欢直接写高精,所以用的重载运算符(一本通第一章STL那节有讲),代码比较长,前面的BIGNUM部分可以直接复制到其它高精题目上(这就是我喜欢它的原因233),代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
using namespace std;
const int MAXN=4000;
struct BIGNUM {
int len,s[MAXN];
BIGNUM () {memset(s,0,sizeof(s)); len=1;}
BIGNUM operator = (const char* num) {
len=strlen(num);
for (int i=0;i<len;++i) s[i]=num[len-i-1]-'0';
return *this;
}
BIGNUM operator = (const int num) {
char a[MAXN];
sprintf(a,"%d",num);
*this = a;
return *this;
}
BIGNUM (const int num) { *this=num; }
BIGNUM (const char * num) { *this=num; }
BIGNUM operator + (const BIGNUM & a) {
BIGNUM c;
c.len=max(len,a.len)+1;
for (int i=0,x=0;i<c.len;++i) {
c.s[i]=s[i]+a.s[i]+x;
x=c.s[i]/10;
c.s[i]=c.s[i]%10;
}
if (c.s[c.len-1]==0) --c.len;
return c;   
}
BIGNUM operator += (const BIGNUM & a) {
*this = *this+a;
return *this;
}
BIGNUM operator * (const BIGNUM & x) {
BIGNUM c;
c.len=len+x.len;
for (int i=0;i<len;++i) {
for (int j=0;j<x.len;++j) {
c.s[i+j]+=s[i]*x.s[j];
c.s[i+j+1]+=c.s[i+j]/10;
c.s[i+j]%=10;
}
}
if (c.s[c.len-1]==0) --c.len;
return c;
}    
BIGNUM operator *= (const BIGNUM & a) {
*this = *this * a;
return *this;
}
bool operator < (const BIGNUM & x) const {
if (len != x.len) return len<x.len;
for (int i=len-1;i>=0;--i) {
if (s[i] != x.s[i]) return s[i]<x.s[i];
}
return false;
}
bool operator > (const BIGNUM & x) const { return x<*this; }
bool operator <= (const BIGNUM & x) const { return !(x<*this); }
bool operator >= (const BIGNUM & x) const { return !(*this<x); }
bool operator == (const BIGNUM & x) const { return !(x<*this||*this<x); }
bool operator != (const BIGNUM & x) const { return x<*this||*this<x; }
};
ostream& operator << (ostream &out,const BIGNUM& x) {
for (int i=x.len-1;i>=0;--i)
cout<<x.s[i];
return out;
}
istream& operator >> (istream &in,BIGNUM &x) {
char num[MAXN];
in>>num;
x=num;
return in;
}
BIGNUM f[5001];
int main ()
{
    BIGNUM sum=1; int n;
    cin>>n;
    for (int i=1;i<=n;i++) sum*=i;
    cout<<sum;
    return 0;
}

这么水的题我竟然写这么长,果然我太弱了QWQ

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值