package myproject;
/**
*
* @author 李瑞琦
* 计算10的阶乘,采用递归算法。
*
*/
public class Test {
static long factorial(int n){
if(n==1){
return 1;
}else{
return n*factorial(n-1);
}
}
public static void main(String[] args) {
long d1 = System.currentTimeMillis();
System.out.println("阶乘的结果: "+factorial(10));
long d2 = System.currentTimeMillis();
System.out.println("递归费时: "+(d2-d1)); //耗时:32ms
}
}
递归算法求10的阶乘
最新推荐文章于 2022-05-17 17:09:15 发布