java编程题,大佬来解决呀
试编程求解:1!+2!-3!+4!+5!-6!+7!+8!-9!+10!+...+20-21!...
试编程求解:1!+2!-3!+4!+5!-6!+7!+8!-9!+10!+...+20-21!
展开
1个回答
展开全部
数值太大,应该使用BigInteger:
public static void main(final String[] args) {
BigInteger sum = BigInteger.ZERO;
BigInteger fac = BigInteger.ONE;
for (int i = 1; i <= 21; ++i) {
fac = fac.multiply(BigInteger.valueOf(i));
if (i % 3 == 0)
sum = sum.subtract(fac);
else
sum = sum.add(fac);
}
System.out.println(sum);
}
结果是:
-48542422041316542099
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询