阶乘的两种实现方式

时间:2017-08-25 14:29:15   收藏:0   阅读:211
public class Util {

/**
* N的阶乘
*
* @param n
* @return
*/
public static int factorial(int n) {
if (n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}

public static int factorial2(int n) {
int val = 1;
for (int i = 2; i <= n; i++) {
val *= i;
}
return val;
}


}

原文:http://www.cnblogs.com/songfahzun/p/7427756.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!