每日一题(70) - n个筛子的点数

题目来自剑指Offer

题目:


思路:

根据题意,需要做两步:

(1)求解点数和s出现的次数,其中s的区间为[n,6n]

(2)求解点数和s出现的概率。

其中难点是求解每一个点数s出现的次数,这里给出两种方法。

思路(1):类似元素的排列,应该算是深度搜索。

(1)n个骰子相当于一个长度为n的一维数组

(2)骰子数字为1-6,则数组的每一个位置都可以为1-6。

代码:

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

void PrintPro(int* pnArrPro,int nDiceNum,int nStart,int nSum)
{
	assert(pnArrPro);
	if (nStart == nDiceNum)
	{
		pnArrPro[nSum - nDiceNum]++;
		return;
	}
	for (int i = 1;i <= 6;i++)
	{
		PrintPro(pnArrPro,nDiceNum,nStart + 1,nSum + i);
	}
}

void PrintPro(int nDiceNum)
{
	//初始化
	int nCount = 6 * nDiceNum - nDiceNum + 1;
	int* pnArrPro = new int[nCount];
	memset(pnArrPro,0,sizeof(int) * nCount);
	
	//求解每个点出现的次数
	PrintPro(pnArrPro,nDiceNum,0,0);
	
	//求解每个点出现的概率
	double nTotal = pow(double(6),nDiceNum);
	int nCur = 0;
	while(nCur < nCount)
	{
		cout<<nDiceNum + nCur<<" "<<pnArrPro[nCur]/nTotal<<endl;
		nCur++;
	}
}

int main()
{
	PrintPro(3);
	system("pause");
	return 1;
}

思路(2):可以使用DP的思想,不过貌似没有最优子结构,姑且认为是DP。

状态转移方程:

F[i,j]:表示由i个骰子投出的点数为j的方案数。

F[n,s] = F[n-1,s-1] + F[n-1,s-2] + F[n-1,s-3] + F[n-1,s-4] + F[n-1,s-5] + F[n-1,s-6]
F[1,s] = 1 。

使用二维数组保存状态

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

/*状态转移方程:
F[n,s] = F[n-1,s-1] + F[n-1,s-2] + F[n-1,s-3] + F[n-1,s-4] + F[n-1,s-5] + F[n-1,s-6]
F[1,s] = 1 */

void SetCount(int** F,int nDiceNum,int nLen)
{
	//初始化
	for (int i = 0;i <= nDiceNum;i++)
	{
		for (int j = 0;j < nLen;j++)
		{
			F[i][j] = 0;
		}
	}
	for (int i = 1;i <= 6;i++)
	{
		F[1][i] = 1;
	}
	//递推
	for (int i = 2;i <= nDiceNum;i++)
	{
		for (int j = i;j <= i * 6;j++)//点数
		{
			for (int m = 1;m < j && m <= 6;m++)
			{
				F[i][j] += F[i - 1][j - m];
			}
		}
	}
}

void PrintPro(int nDiceNum)
{
	//初始化
	int nLen = nDiceNum * 6 + 1;
	int** F = new int*[nDiceNum + 1];//点数和数组
	for (int i = 0;i <= nDiceNum;i++)
	{
		F[i] = new int[nLen];
	}
	//求解每个点出现的次数
	SetCount(F,nDiceNum,nLen);

	//求解每个点出现的概率
	double nTotal = pow(double(6),nDiceNum);
	int nCur = nDiceNum;
	while(nCur < nLen)
	{
		cout<<nCur<<" "<<F[nDiceNum][nCur]/nTotal<<endl;
		nCur++;
	}
}

int main()
{
	PrintPro(4);
	system("pause");
	return 1;
}
使用一维数组保存状态

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

/*状态转移方程:
F[n,s] = F[n-1,s-1] + F[n-1,s-2] + F[n-1,s-3] + F[n-1,s-4] + F[n-1,s-5] + F[n-1,s-6]
F[1,s] = 1 */

void SetCount(int* F,int nDiceNum,int nLen)
{
	//初始化
	memset(F,0,sizeof(int) * nLen);
	for (int i = 1;i <= 6;i++)
	{
		F[i] = 1;
	}
	//递推
	for (int i = 2;i <= nDiceNum;i++)
	{
		for (int j = i * 6;j >= i;j--)//点数
		{
			F[j] = 0;
			for (int m = 1;m <= j - i + 1 && m <= 6;m++)
			{
				F[j] += F[j - m];
			}
		}
	}
}

void PrintPro(int nDiceNum)
{
	//初始化
	int nLen = nDiceNum * 6 + 1;
	int* F = new int[nLen];//点数和数组

	//求解每个点出现的次数
	SetCount(F,nDiceNum,nLen);

	//求解每个点出现的概率
	double nTotal = pow(double(6),nDiceNum);
	int nCur = nDiceNum;
	while(nCur < nLen)
	{
		cout<<nCur<<" "<<F[nCur]/nTotal<<endl;
		nCur++;
	}
}

int main()
{
	PrintPro(3);
	system("pause");
	return 1;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值