另一种阶乘问题

另一种阶乘问题

时间限制: 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
来源
[张洁烽]原创
上传者

张洁烽


问题链接: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. }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值