1. 求100!的结果的各位数之和为多少?
如:5!=5*4*3*2*1=120,那么他们的和为1+2+0=3
<span style="white-space:pre"> </span>@Test
public void test1() {
BigInteger fact = getFact(100);
System.out.println(getSum(fact));
}
public BigInteger getFact(int n) {
if (n == 1) {
return new BigInteger("1");
}
return getFact(n - 1).multiply(new BigInteger(n + ""));
}
public int getSum(BigInteger num) {
String fact = num.toString();
int sum = 0;
for (int i = 0; i < fact.length(); i++) {
sum += Integer.parseInt(fact.charAt(0) + "");
}
return sum;
}
本文介绍了一种使用Java编程语言计算100的阶乘(100!)后,其结果各位数字之和的方法。通过递归计算阶乘并采用BigInteger类型来处理大数值运算,最后遍历每一位数字并求和。
&spm=1001.2101.3001.5002&articleId=42155899&d=1&t=3&u=6014eb6f218f4a8fbe413289891ed841)
2万+

被折叠的 条评论
为什么被折叠?



