求N的阶层

第一种方法:

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;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值