另一种阶乘问题

来源:互联网 发布:信封袋设计的软件 编辑:程序博客网 时间:2024/06/06 03:36

另一种阶乘问题

时间限制: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!!的正确值
样例输入
235
样例输出
523
来源
[张洁烽]原创
上传者

张洁烽


问题链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=65

代码:

#include <iostream>#include <stdio.h> #include <string.h>#include <math.h>#include <vector>#include <queue>#include <stack>#include <map>#include <string>#include <algorithm>using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */int factorial(int n){int ans=1;for(int i=3;i<n;i+=2){ans*=i;}if(n%2 == 1){ans*=n;}return ans;}int main(int argc, char** argv) {int a;scanf("%d",&a);while(a--){int n;scanf("%d",&n);int ans=0; for(int i=1;i<=n;i++){ans+=factorial(i);}printf("%d\n",ans);}return 0;}

优秀代码:

01./*
02.#include<iostream>
03.using namespace std;
04.int f(int n)
05.{
06.if(n%2) return n==1?1:n*f(n-2);
07.return f(n-1);
08.}
09.int g(int n)
10.{
11.return n?g(n-1)+f(n):0;
12.}
13.int main()
14.{
15.int n,m;
16.cin>>n;
17.while(n--)
18.{
19.cin>>m;
20.cout<<g(m)<<endl;
21.}
22.}*/
23.#include<iostream>
24.using namespace std;
25.int main()
26.{
27.int n,m,r[]={0,1,2,5,8,23,38,143,248,1193,2138,12533,22928,158063,293198,2320223,4347248,38806673,73266098,727995173,1382724248};
28.cin>>n;
29.while(n--)
30.{
31.cin>>m;
32.cout<<r[m]<<endl;
33.}
34.}