在Java中,求1!+2!+3!+···+1000!的结果,结果长度远远超出int和long类型的最大值,所以要使用BigDecimal类型,代码如下:
1 BigDecimal count = new BigDecimal(0); 2 for (int i = 1; i <= 1000; i++) { 3 BigDecimal mutil = new BigDecimal(1); 4 for (int j = 1; j <= i; j++) { 5 mutil = mutil.multiply(new BigDecimal(j)); 6 } 7 count = count.add(mutil); 8 } 9 System.out.println("值:" + count.toString());
时间: 2025-01-16 07:11:15