码迷,mamicode.com
首页 > 编程语言 > 详细

武汉科技大学ACM :1002: 零起点学算法38——求阶乘和

时间:2014-12-09 22:50:50      阅读:544      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   sp   for   strong   on   

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\n",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\n",sum);
18     }
19     return 0;
20 }

 

武汉科技大学ACM :1002: 零起点学算法38——求阶乘和

标签:des   style   blog   io   color   sp   for   strong   on   

原文地址:http://www.cnblogs.com/liuwt365/p/4154129.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!