九度 Online Judge 算法 刷题 题目1067:n的阶乘

时间:2023-02-12 12:40:05

题目1067:n的阶乘

题目描述:
输入一个整数n,输出n的阶乘
输入:
一个整数n(1<=n<=20)
输出:
n的阶乘
样例输入:
3
样例输出:
6
来源:
2001年清华大学计算机研究生机试真题(第II套)

code

java 实现

import java.util.Scanner;
 
public class Main {
    public static long count(int num){
        if (num == 1)
            return 1; 
        else
            return num*count(num-1);
    }    
     
    public static void main(String[] args) {
        Scanner shuru = new Scanner(System.in);
        while(shuru.hasNext())
        {
                int n = shuru.nextInt();
                System.out.println(count(n));
        }
    }
}
 
/**************************************************************     Problem: 1067     User: langzimaizan     Language: Java     Result: Accepted     Time:90 ms     Memory:15480 kb ****************************************************************/