洛谷千题详解 | P1009 [NOIP1998 普及组] 阶乘之和【C++、Java、Python、Pascal语言】
博主主页:Yu·仙笙
专栏地址:洛谷千题详解
目录
题目描述
输入格式
输出格式
输入输出样例
解析:
C++源码:
Python源码:
Java源码:
Pascal源码:
-------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------
题目描述
用高精度计算出 S=1!+2!+3!+⋯+n!(n≤50)。
其中 !
表示阶乘,定义为n!=n×(n−1)×(n−2)×⋯×1。例如,5!=5×4×3×2×1=120。
-------------------------------------------------------------------------------------------------------------------------------
输入格式
一个正整数 n。
-------------------------------------------------------------------------------------------------------------------------------
输出格式
一个正整数 S,表示计算结果。
-------------------------------------------------------------------------------------------------------------------------------
输入输出样例
输入 #1
3
输出 #1
9
-------------------------------------------------------------------------------------------------------------------------------
解析:
思路就是高精乘+高精加,就是把高精乘的模板套上去接着套高精加的模板,b=c=i的阶乘。
-------------------------------------------------------------------------------------------------------------------------------
C++源码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
struct fantastic //嗯,开始重载了
{
int len,s[9999];
fantastic()
{
memset(s,0,sizeof(s));
len=1;
}
fantastic operator=(const char*num)
{
len=strlen(num);
for(int i=0;i<len;++i)
s[i]=num[len-i-1]-'0';
return *this;
}
fantastic operator=(const int num)
{
char a[9999];
sprintf(a,"%d",num);
*this=a;
return *this;
}
fantastic (const int num)
{
*this=num;
}
fantastic (const char * num)
{
*this=num;
}
fantastic operator+(const fantastic &a) //这里在重载 “+” 的运算
{
fantastic 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;
}
fantastic operator * (const fantastic &x) //然后再来波 “*” 的运算
{
fantastic 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;
}
};
ostream& operator<<(ostream &out,const fantastic& x) //重载一下输出
{
for(int i=x.len-1;i>=0;--i)
cout<<x.s[i];
return out;
}
istream& operator>>(istream &in,fantastic &x) //重载一下输入
{
char num[9999];
in>>num;
x=num;
return in;
}
int main() //然后就可以愉快的开始主程序啦
{
int n;
fantastic ans=0,num=1;
cin>>n;
for(int i=1;i<=n;i++)
{
num=num*i;
ans=ans+num;
}
cout<<ans<<endl;
} //非常的简单明了
-------------------------------------------------------------------------------------------------------------------------------
Python源码:
print(reduce(lambda x,y:x+y,[reduce(lambda x,y:x*y,range(1,i+1)) for i in range(1, int(raw_input())+1)]))
-------------------------------------------------------------------------------------------------------------------------------
Java源码:
private static final BigInteger[] INTEGERS = new BigInteger[51];
static {
INTEGERS[0] = new BigInteger("1");
}
public static void main(String[] args) {
BigInteger result = new BigInteger("0");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int i = 1; i <= n; ++ i) {
INTEGERS[i] = INTEGERS[i - 1].multiply(new BigInteger(String.valueOf(i)));
result = result.add(INTEGERS[i]);
}
System.out.println(result);
}
-------------------------------------------------------------------------------------------------------------------------------
Pascal源码:
var a,b:array[1..1000] of int64;
i,j,la,lb,n:longint;
procedure cheng(t:longint);//高精度乘单精度
var i:longint;
begin
for i:=1 to la do
a[i]:=a[i]*t;
for i:=1 to la do
begin
a[i+1]:=a[i+1]+a[i] div 10;
a[i]:=a[i] mod 10;
end;
while a[la+1]>0 do inc(la);
end;
procedure jia;//高精度加法
var i:longint;
begin
for i:=1 to la do
begin
b[i]:=b[i]+a[i];
b[i+1]:=b[i+1]+b[i] div 10;
b[i]:=b[i] mod 10;
end;
while b[lb+1]>0 do inc(lb);
end;
begin
read(n);
a[1]:=1;
for i:=1 to n do
begin
cheng(i);
jia;
end;
for i:= lb downto 1 do
write(b[i]);
end.
-------------------------------------------------------------------------------------------------------------------------------