841
人物
另一種階乘問題
另一種階乘問題時間限製:3000 ms | 內存限製:65535 KB
難度:1
描述
大家都知道階乘這個概念,舉個簡單的例子:5!=1*2*3*4*5.現在我們引入一種新的階乘概念,將原來的每個數相乘變為i不大於n的所有奇數相乘例如:5!!=1*3*5.現在明白現在這種階乘的意思了吧!
現在你的任務是求出1!!+2!!......+n!!的正確值(n<=20)
輸入
第一行輸入一個a(a<=20),代表共有a組測試數據
接下來a行各行輸入一個n.
輸出
各行輸出結果一個整數R表示1!!+2!!......+n!!的正確值
樣例輸入
2
3
5
樣例輸出
5
23
my code as follows:
01.
#include <iostream>
02.
03.
using
namespace
std;
04.
14.
15.
int
main()
16.
{
17.
18.
int
samples;
//測試數據組數
19.
cin >> samples;
20.
while
(samples--)
21.
{
22.
int
n;
23.
cin >> n;
24.
25.
long
long
sum = 0;
26.
long
long
mul = 1;
27.
for
(
int
i = 1; i <= n; i++)
28.
{
29.
if
(i % 2 != 0)
//if
is odd
30.
mul *= i;
31.
sum += mul;
32.
}
33.
cout << sum << endl;
34.
35.
}
36.
37.
return
0;
38.
}
最後更新:2017-04-02 15:14:52