题目链接
题解
用一个jj表示i-1的阶乘.
就不用每次重新计算新的阶乘.
最后求和统计答案即可.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> using namespace std ; template<class T>void read(T &x){ x=0;int f=0;char ch=getchar(); while(ch<'0'||ch>'9'){f|=(ch=='-');ch=getchar();} while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} x=f?-x:x; return ; } int main() { int a,sum=0,jj=1;read(a); for(int i=1;i<=a;++i){ jj*=i;sum+=jj; } printf("%d",sum); return 0; } |
By:Wahacer
2017.11.22
21:29