您的位置:首页 > 其它

题目1179:阶乘

2015-05-01 13:46 309 查看
题目描述:
输入n,
求y1=1!+3!+...m!(m是小于等于n的最大奇数)
y2=2!+4!+...p!(p是小于等于n的最大偶数)。

输入:
每组输入包括1个整数:n

输出:
可能有多组测试数据,对于每组数据,
输出题目要求的y1和y2

样例输入:
4

样例输出:
7 26


import java.math.BigInteger;
import java.util.Scanner;

public class Main{
public static void main(String[]args){
Scanner in=new Scanner(System.in);
long[]re=new long[10001];
for(int i=1;i<re.length;i++)
re[i]=1;
for(int i=1;i<re.length;i++){
for(int j=1;j<=i;j++){
re[i]*=j;
}
}
while(in.hasNext()){
int n=in.nextInt();
BigInteger y1=new BigInteger("0");
BigInteger y2=new BigInteger("0");
for(int i=1;i<=n;i++){
BigInteger an=JC(i);
if(i%2!=0){
y1=y1.add(an);
}
else{
y2=y2.add(an);
}
}
System.out.println(y1+" "+y2);
}
}
public static BigInteger JC(int n){
BigInteger an=new BigInteger("1");
for(int i=1;i<=n;i++){
BigInteger tem=new BigInteger(String.valueOf(i));
an=an.multiply(tem);
}
return an;
}
}

/**************************************************************
Problem: 1179
User: 0000H
Language: Java
Result: Accepted
Time:290 ms
Memory:17448 kb
****************************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: