码迷,mamicode.com
首页 > 其他好文 > 详细

阶乘的两种实现方式

时间:2017-08-25 15:01:47      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:stat   tor   阶乘   int   ati   ram   else   ++   ==   

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;
}


}

阶乘的两种实现方式

标签:stat   tor   阶乘   int   ati   ram   else   ++   ==   

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

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