Problem Description
输入一个正整数n(n<=10),计算
S=1!+2!+3!+...+n!
Input
输入一个正整数n(n<=10)(多组数据)
Output
输出S(每组数据一行)
Sample Input
2
Sample Output
3
#include<stdio.h> int main() { long int s,a; int i,n; while(scanf("%d",&n)!=EOF) { s=0; a=1; for(i=1;i<=n;i++) { a=a*i; s=s+a; } printf("%ld ",s); } return 1; }
其他代码:
1 #include<stdio.h> 2 int ridsum(int n) 3 { 4 int sum,i; 5 if(n==0||n==1) 6 return 1; 7 else 8 return ridsum(n-1)*n; 9 } 10 int main() 11 { 12 int n,sum,i; 13 while(scanf("%d",&n)!=EOF) 14 { 15 for(i=1,sum=0;i<=n;i++) 16 sum+=ridsum(i); 17 printf("%d ",sum); 18 } 19 return 0; 20 }