n的阶乘
返回值阶乘结果的位数
注意:
本程序直接输出n!的结果,需要返回结果请保留long a[]
import java.util.Scanner;
public class 大数阶乘 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true) {
int n = sc.nextInt();
int result = factorical(n);
System.out.println("阶乘结果的位数" + result);
}
}
public static int factorical(int n) {
long[] a = new long[10000];
int i,j,l,c,m=0,w;
a[0] = 1;
for(i = 1;i <= n;i++) {
c = 0;
for(j=0;j <= m;j++) {
a[j] = a[j]*i + c;
c = (int)(a[j] / 10000);
a[j] = a[j] % 10000;
}
if(c > 0) {
m++;
a[m] = c;
}
}
w = (int)(m * 4 + Math.log10(a[m]) + 1);
System.out.print(a[m]);
for(i = m - 1;i >= 0; i--) {
String s = a[i] + "";
while(s.length() < 4) {
s = "0" + s;
}
System.out.print(s);
}
System.out.println();
return w;
}
}