int result = 1
for (int i=1i<=numi++){
result *= i
}
此时result的值即为指迹睁num的阶乘(仅正整州消数)
class test{ public static void main(String[] args){ fun f=new fun()for(int i=0i<=5i++) System.out.print(f.fact1(i)+" ")System.out.println()for(int i=0i<=5i++) System.out.print(f.fact2(i)+" ")System.out.println()long [] p=f.fact3(5)for(int i=0i<p.lengthi++) System.out.print(p[i]+" ")System.out.println()} } class fun{ long fact1(int n){ long result=1if(n==0)return 0for(int i=1i<=ni++) result=result*ireturn result} long fact2(int n){ if(n==0)return 0if(n==1)return 1else return n*fact2(n-1)} long [] fact3(int n){ long [] result=new long[n+1]result[0]=0result[1]=1for(int i=2i<=ni++) result[i]=result[i-1]*ireturn result} }欢迎分享,转载请注明来源:内存溢出
评论列表(0条)