程序员面试题精选100题(43)-n个骰子的点数

// 程序员面试题精选100题(43)-n个骰子的点数.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <windows.h>
using namespace std;
//
int g_maxValue = 6;

void SumProbabilityOfDices(int original, int current, int value, int tempSum, int* pProbabilities)
{
	if(current == 1)
	{
		int sum = value + tempSum;
		pProbabilities[sum - original]++;
	}
	else
	{
		for(int i = 1; i <= g_maxValue; ++i)
		{
			int sum = value + tempSum;
			SumProbabilityOfDices(original, current - 1, i, sum, pProbabilities);
		}
	}
} 
void SumProbabilityOfDices(int number, int* pProbabilities)
{
	for(int i = 1; i <= g_maxValue; ++i)
		SumProbabilityOfDices(number, number, i, 0, pProbabilities);
}

void PrintSumProbabilityOfDices_1(int number)
{
	if(number < 1)
		return;

	int maxSum = number * g_maxValue;
	int* pProbabilities = new int[maxSum - number + 1];
	for(int i = number; i <= maxSum; ++i)
		pProbabilities[i - number] = 0;

	SumProbabilityOfDices(number, pProbabilities);

	int total = pow((double)g_maxValue, number);
	double sum1=0;
	for(int i = number; i <= maxSum; ++i)
	{
		double ratio = (double)pProbabilities[i - number] / total;
		//printf("%d: %lf\n", i, ratio);
		//cout<<i<<":"<<ratio<<" ";
		cout<<i<<":"<<pProbabilities[i - number]<<" ";
		sum1+=ratio;
	}
	cout<<"sum is equal to "<<sum1<<endl;
	cout<<"total is equal to "<<total<<endl;
	delete[] pProbabilities;
}
///
void PrintSumProbabilityOfDices_2(int number)
{
	double* pProbabilities[2];
	pProbabilities[0] = new double[g_maxValue * number + 1];
	pProbabilities[1] = new double[g_maxValue * number + 1];
	for(int i = 0; i < g_maxValue * number + 1; ++i)
	{
		pProbabilities[0][i] = 0;
		pProbabilities[1][i] = 0;
	}

	int flag = 0;
	for (int i = 1; i <= g_maxValue; ++i)
		pProbabilities[flag][i] = 1;

	for (int k = 2; k <= number; ++k)
	{
		for (int i = k; i <= g_maxValue * k; ++i)
		{
			pProbabilities[1 - flag][i] = 0;
			for(int j = 1; j <= i && j <= g_maxValue; ++j)
				pProbabilities[1 - flag][i] += pProbabilities[flag][i - j];
		}

		flag = 1 - flag;
	}

	double total = pow((double)g_maxValue, number);
	double ifone=0;
	for(int i = number; i <= g_maxValue * number; ++i)
	{
		double ratio = pProbabilities[flag][i] / total;
		ifone+=ratio;
		//?printf("%d: %f\n", i, ratio);
		cout<<i<<":"<<pProbabilities[flag][i]<<" ";
	}
	cout<<" all the probabilities is "<<ifone<<endl;
	delete[] pProbabilities[0];//local , here is right
	delete[] pProbabilities[1];
}
//
void PrintSumProbabilityOfDices_myself(int n)// all by myself without looking the answer, actually is the idea of the second method,but is seems that the second answer is wrong somewhere
{
	
	//?HANDLE hHeap1,hHeap2,hHeap3;
	int *dicesArr = NULL;
	dicesArr = new int[n*7+1];//int[n*6+1]; here i made a so stupid mistake which takes me nearly 4 hour to handle,all the notes parts are my attempts to correct my program, indeed, I learn a little thing  
	//hHeap1 = HeapCreate(HEAP_GENERATE_EXCEPTIONS,0x1000,0xffff);
	//dicesArr = (int*)HeapAlloc(hHeap1,HEAP_NO_SERIALIZE, sizeof(int)*(7*n+1));//Number of bytes to be allocated.that is means that if dicesArr access out of range HeapFree is wrong
	int *dicesSum = NULL;
	dicesSum = new int[n*7+1];
	//hHeap2 = HeapCreate(HEAP_GENERATE_EXCEPTIONS,0x1000,0xffff);
	//dicesSum = (int*)HeapAlloc(hHeap2,HEAP_NO_SERIALIZE, sizeof(int)*(7*n+1));
	//hHeap3 = HeapCreate(HEAP_GENERATE_EXCEPTIONS,0x1000,0xffff);
	double *problility = NULL;
	problility = new double[7*n+1];
	//problility = (double*)HeapAlloc(hHeap3,HEAP_NO_SERIALIZE, sizeof(double)*(7*n+1));// Fuck!, so easy a sentence really takes me more than 3 hours
	for (int i=0;i<=n*6;i++)
	{
		dicesArr[i]=0;
		dicesSum[i]=0;
	}
	for (int i=1;i<=6;i++)
	{
		dicesArr[i]=1;
		dicesSum[i]=dicesSum[i-1]+dicesArr[i];
	}
	for (int k=2;k<=n;k++)
	{
		for (int i=k;i<=7*k/2;i++)//dicesArr[0] dicesSum[0] is always 1
		{
			if(i-k>=6)
			{
				dicesArr[i]=dicesSum[i-1]-dicesSum[i-1-6];
			}
			else
			{
				dicesArr[i]=dicesSum[i-1];
			}
			dicesArr[7*k-i]=dicesArr[i];
		}

		for (int j=k;j<=7*k;j++)
		{
			if (j==k)
			{
				dicesSum[j]=1;
			}
			else
			dicesSum[j]=dicesSum[j-1]+dicesArr[j];
		}
		/*
		for (int i=k;i<=6*k;i++)
		{
			cout<<dicesArr[i]<<":";
			cout<<dicesSum[i]<<" ";
		}
		cout<<endl;*/
	}
	double sum=0;
	long int sumint=0;
	for (int j=n;j<=6*n;j++)
	{
		problility[j] = dicesArr[j]/pow(6.0,n);
		sumint+=dicesArr[j];
		sum+=problility[j];
		//cout<<j<<":"<<problility[j]<<" ";
		cout<<j<<":"<<dicesArr[j]<<" ";
	}
	cout<<"sum is "<<sum<<endl;
	cout<<sumint<<" is equal to "<<pow(6.0,n)<<endl;
	/**/
	if (dicesArr!=NULL)
	{
		delete[] dicesArr;
		//HeapFree(hHeap1,0,dicesArr);// here is noticeable when the 
		dicesArr = NULL;//dicesArr = NULL; 居然不报错 dicesArr[] = NULL; 报错
	}
	//new分配的内存是从堆上的来的,而局部变量都是从栈上得到内存。每个模块都有一个与之相绑定的堆和一个栈,
	//其两个的大小都可以通过编译器选项指定。所以楼主的new是从dll的堆上分,delete是从应用程序的堆上撤,根本不是同一个堆上,所以出错。
	
	if (dicesSum!=NULL)
	{
		delete[] dicesSum;//here is wrong even in the local function,but in the above functions both are right,why????
		//HeapFree(hHeap2,0,dicesSum);
		dicesSum = NULL;
	}
	if (problility!=NULL)
	{
		delete[] problility;
		//HeapFree(hHeap3,0,problility);
		problility = NULL;
	}
	
}

//
int _tmain(int argc, _TCHAR* argv[])
{
	int n;
	cin>>n;
	PrintSumProbabilityOfDices_1(n);
	//PrintSumProbabilityOfDices_2(n);
	cout<<"what?"<<endl;
	PrintSumProbabilityOfDices_myself(n);
	system("pause");

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值