唱着歌屠龙
机器学渣
 
 

  n!阶乘问题

 
 
时间限制:10秒    内存限制:256兆

题目描述

 阶乘 n!  

n 的阶乘  (factorial)   可表示为 f(n) = n! = n*(n-1)*(n-2)*…*2*1. ,其中(n <= 20)
 
请编写函数,使用递归的方法求 n! ,在主函数中输入一个整数 n,调用这个函数,
测试函数是否正确。 
 
请完善以下的阶乘函数double factoral(int n),以递归的方式实现。
只要提交函数实现即可:
 
double factoral(int n)
{
        // 完善你的函数实现,采用递归函数的方式,不要用循环迭代的方式
}
 
循环方式实现请回去后以作业形式上交。
 
 
 

输入格式

 none

输出格式

 none

样例输入

1
2

样例输出

1
2
 1 // Problem#: 6375
 2 // Submission#: 1632077
 3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
 4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
 5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University
 6 
 7 #include <stdio.h>
 8 
 9 double factoral(int n);
10 int main()
11 {
12     
13     int i;
14 
15     int n;
16 
17     double sum;
18 
19     scanf("%d", &n);
20 
21     sum = factoral(n);
22 
23     printf("%.0lf\n", sum);
24   
25    return 0;    
26 }
27 double factoral(int n)
28 {
29        if(n <= 1)
30            return 1;
31        else
32            return  n * factoral(n-1) ;
33 }                             

 

posted on 2012-11-07 23:39  唱一支歌  阅读(164)  评论(0编辑  收藏  举报