HDOJ:【6】2048神、上帝以及老天爷

原题目:

http://acm.hdu.edu.cn/showproblem.php?pid=2048

主要思路:

我认为这道题主要有以下三个需要注意的地方:

  1. 排列数公式
  2. 错排公式,和2049题目一样
  3. 小数点后两位的四舍五入。(我的实现方法是,先将这个小于1的double数乘以10000,对其进行四舍五入取整,再除以100变成百分数)

源代码:

#include <iostream>
#include <math.h>
using namespace std;

unsigned long long int stairs(unsigned long long int X)
{
	if(X == 0)
	{
		return 1;
	}
	unsigned long long int i = 1;
	unsigned long long int result = 1;
	for(i = 1; i <= X; i++)
	{
		result *= i;
	}
	return result;
} 

unsigned long long int A(unsigned long long int m,unsigned long long int n)
{
	unsigned long long int result;
	result = stairs(n)/stairs(n-m);
	return result;
}

int main()
{
	unsigned long long int c;
	unsigned long long int n[25];
	double result[25];
	unsigned long long int cuo[21];
	cuo[1] = 0;
	cuo[2] = 1;
	
	for(int i = 3; i <= 20; i++)
	{
		cuo[i] = (i-1)*(cuo[i-1] + cuo[i-2]);
	}
	
	scanf("%llu", &c);
	for(int i = 0; i < c; i++)
	{
		scanf("%llu",&n[i]);
	}
	
	for(int i = 0; i < c; i++)
	{
		result[i] = (double)cuo[n[i]]/A(n[i],n[i]);
	}
	
	for(int i = 0; i < c; i++)
	{
		result[i] *= 10000;
		result[i] = round(result[i]);
		result[i] = result[i]/100.0;
		printf("%.2lf%%\n",result[i]);
	}
	return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alex-YiWang

不要打赏,想要一个赞

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值