一、题目链接
http://noi.openjudge.cn/ch0106/14/
二、解题思路
◎ 根据题意,n!=基数1循环乘上2、3、4、…、n;
◎ 从低位到高位,用当前结果的每一位数字乘上当前乘数,同时加上进位,形成新的计算结果。
三、实施步骤
四、Java程序
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public int[] factorial(int n) {
int[] ans = new int[35660];
ans[0] = 1;
int length = 1;
int carry;
int i;
int j;
for (i = 2; i <= n; i++) {
carry = 0;
for (j = 0; j < length; j++) {
ans[j] = carry + ans[j] * i;
carry = (ans[j] > 9 ? ans[j] / 10 : 0);
ans[j] = (ans[j] > 9 ? ans[j] % 10 : ans[j]);
}
while (carry > 0) {
ans[length] = (carry > 9 ? carry % 10 : carry);
length++;
carry = carry / 10;
}
}
for (i = 0; i < length / 2; i++) {
carry = ans[i];
ans[i] = ans[length - i - 1];
ans[length - i - 1] = carry;
}
return Arrays.copyOfRange(ans, 0, length);
}
public static void main(String[] args) {
Main test = new Main();
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] ans = test.factorial(n);
for (int i : ans) {
System.out.print(i);
}
}
}