-
题目描述:
-
输入n,
求y1=1!+3!+...m!(m是小于等于n的最大奇数)
y2=2!+4!+...p!(p是小于等于n的最大偶数)。
-
输入:
-
每组输入包括1个整数:n
-
输出:
-
可能有多组测试数据,对于每组数据,
输出题目要求的y1和y2
-
样例输入:
-
4
-
样例输出:
-
7 26
-
来源:
- 2000年华中科技大学计算机研究生机试真题
-
import java.math.BigInteger; import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner in=new Scanner(System.in); while(in.hasNext()){ int n=in.nextInt(); BigInteger sum1=BigInteger.ZERO,sum2=BigInteger.ZERO; for(int i=1;i<=n;i++){ if(i%2==0){ BigInteger a=BigInteger.ONE; for(int j=1;j<=i;j++) a=a.multiply(BigInteger.valueOf(j)); sum2=sum2.add(a); } else { BigInteger a=BigInteger.ONE; for(int j=1;j<=i;j++) a=a.multiply(BigInteger.valueOf(j)); sum1=sum1.add(a); } } System.out.println(sum1+" "+sum2); } in.close(); } }