UVA-10759

题意:

       n个骰子点数之和大于等于x的概率为多少。

题解:

      这道题第一眼看上去就是DP。思路很简单,DP[n][x]表示n个筛子点数恰好为x的情况数。

DP[n][x] = DP[n-1][x-1] + DP[n-1][x-2] +DP[n-1][x-3] + DP[n-1][x-4] +  DP[n-1][x-5] + DP[n-1][x-6] 

#include <bits/stdc++.h>  
using namespace std;
long long gcd(long long a, long long b)
{
	return b == 0 ? a : gcd(b, a % b);
}
int main()
{
	long long DP[30][160] = { 0 };
	for (int x = 1; x < 160; x++)DP[0][x] = x;
	for (int x = 1; x <= 6; x++)DP[1][x] = 1;
	for (int n = 2; n <= 24; n++) for (int x = 1; x <= 150; x++)
		for (int k = 1; k <= 6; k++) if (x - k >= 1)	DP[n][x] += DP[n - 1][x - k];
	int n, x;
	while (cin >> n >> x, n)
	{
		cout << fixed;
		long long lt = 0;  for (int i = x; i <= 150; i++) lt += DP[n][i];
		long long tt = pow(6, n);
		long long g = gcd(lt, tt);
		lt /= g;    tt /= g;
		if (lt == 0 || tt == 1) cout << lt << endl;
		else  cout << lt <<"/"<<tt<< endl;

	}
	return 0;
}

 

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页