描述
大家都知道阶乘这个概念,举个简单的例子:5!=1*2*3*4*5.现在我们引入一种新的阶乘概念,将原来的每个数相乘变为i不大于n的所有奇数相乘例如:5!!=1*3*5.现在明白现在这种阶乘的意思了吧!
现在你的任务是求出1!!+2!!......+n!!的正确值(n<=20)
2 3 5
5 23
import java.util.*;
public class 另一种阶乘问题 {
public static void main(String[] args) throws Exception {
Scanner a = new Scanner(System.in);
int n = a.nextInt();
int num = 0;
int sum = 0;
int[] result = new int[n+1];
int p =1;
for (int i = 1; i <= n; i++) {
num = a.nextInt();
for (int j = 1; j <= num; j++) {
for (int k = 1; k<=j; k+=2) {
p = p * k;
}
sum = sum + p;
p = 1;
}
result[i-1] = sum;
sum = 0;
}
for (int i = 0; i < n; i++) {
System.out.println(result[i]);
}
}
}
原文:http://545283613.iteye.com/blog/2246622