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