骰子求和 c++

  • Post author:
  • Post category:其他


#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;
	}
	
}



版权声明:本文为mengxiangyuanfang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。