1. 程式人生 > >骰子求和 c++

骰子求和 c++

#include<iostream>
#include<iomanip>
#define MAXLENGTH 100
using namespace std;

void dict(int* result, int num, int sum)
{
	if (num == 0)
	{
		//int b = result[sum];
		result[sum] += 1;
		
	}
	else
	{
		for (int i = 1; i <= 6; i++)
		{
			sum += i;
			dict(result, --num, sum);
			sum -= i;
			++num;
		}
	}

}

int main()
{
	int n;
	int result[MAXLENGTH];
	memset(result, 0, sizeof(result));
	cin >> n;
	cin.clear();
	cin.sync();
	dict(result, n, 0);
	for (int i = n; i <= n * 6; i++)
	{
		cout <<'['<< i<<']'<< setprecision(2) << (1.0*result[i])/ pow(6.0, n) << endl;
	}
	
}