HDU - 1042-N!(Java)

本文介绍了一个使用Java实现的大数阶乘计算程序。该程序能够处理0到10000之间的整数N,并准确计算其阶乘。通过使用BigDecimal类,确保了计算的精度,适用于需要高精度数学运算的场景。

Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! 

Input

One N in one line, process to the end of file. 

Output

For each N, output N! in one line. 

Sample Input

1
2
3

Sample Output

1
2
6

代码:

 

import java.math.BigDecimal;
import java.util.Scanner;
 
public class Main {
 
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int n = sc.nextInt();
			System.out.println(fun(n));
		}
	}
 
	public static BigDecimal fun(int n) {
		BigDecimal s = new BigDecimal(1);
		for (int i = 1; i <= n; i++) {
			BigDecimal a = new BigDecimal(i);
			s = s.multiply(a);
		}
		return s;
	}
 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

black-hole6

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值