第一种方法:
第二种方法:
int[] res = new int[3000];
final int limit = 5;
res[1] = 1;
int max_now = 1;
for (int step = 1; step <= limit; step++) {
int temp = 0;
int now = max_now;
int zero;
for (zero = 1; zero <= now; zero++) {
if (res[zero] != 0)
break;
}
for (int carry = zero - 1; carry <= now; carry++) {
res[carry] *= step;
res[carry] += temp;
temp = 0;
if (res[carry] >= 10) {
int carry_temp = carry;
temp = res[carry];
if (carry_temp <= max_now) {
res[carry_temp] = temp % 10;
temp /= 10;
carry_temp++;
}
if (carry_temp > max_now) {
while (temp >= 10) {
res[carry_temp] = temp % 10;
temp /= 10;
carry_temp++;
}
res[carry_temp] = temp;
temp = 0;
max_now = carry_temp;
}
}
}
}
for (int j = max_now; j > 0; j--) {
System.out.print(res[j]);
}
第二种方法:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
public class JieCeng {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入一个整数:");
try {
int a = Integer.parseInt(br.readLine());
System.out.println(a + "!=" + SuanFa(a));
} catch (NumberFormatException e) {
System.out.println("您输入的数有误!");
} catch (IOException e) {
System.out.println("您输入的数有误!");
}
}
public static BigInteger SuanFa(int b) {
BigInteger sum = new BigInteger("1");
for (; b > 0; b--) {
sum = sum.multiply(new BigInteger(new Integer(b).toString()));
}
return sum;
}
}