public static void main(String args[]) {
int x = 5
int rs = Fac(x)
System.out.println("贺喊衫" + x + "! = " + rs)
}
public static long Fac(int x) { /*阶乘算渗袭法*/
if (x >1)
return (x * Fac(x - 1))/禅腔*递归*/
else
return 1
}
}
public static void main(String[] args){念含梁 System.out.println("输入n,按回车:")
Scanner s = new Scanner(System.in)
Integer n = Integer.parseInt(s.next())
Integer re = factorial(1, n)
System.out.println(re)
}
public static Integer factorial(Integer a, Integer b){
if(b == 1){
return a * b
}else{
return a * b * factorial(a, b-1)
仔运 }
}
这个最后吐槽一句,用循环一下就解决了,非要用递归,闲得蛋疼老虚
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)