hihocoder太阁面经算法竞赛8-Dice Possibility
2016-07-11 23:55
302 查看
hihocoder太阁面经算法竞赛8-Dice Possibility。
题目:
What is possibility of rolling N dice and the sum of the numbers equals to M?
Two integers N and M. (1 ≤ N ≤ 100, 1 ≤ M ≤ 600)
Output the possibility in percentage with 2 decimal places.
样例输入
样例输出
这道题比较简单,使用模拟的方法,即可解决。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int maxn = 605;
const int maxm = 102;
const double min_val = 1e-9;
int n,m;
double mp[maxm][maxn];
int main(){
freopen("in.txt", "r", stdin);
int i,j,k, cnt;
while(scanf("%d %d", &n, &m) != EOF){
if(n > m || 6*n < m){
printf("%.2f\n", 0.00);
continue;
}
memset(mp, 0, sizeof(mp));
mp[0][0] = 1.0; cnt = 0;
for(i=1; i<=n; i++){
for(j=cnt; j<=6*cnt; j++){
for(k=1; k<=6; k++){
mp[i][j+k] += mp[i-1][j]*(1.0/6.0);
}
}
cnt++;
}
printf("%.2f\n", mp
[m]*100);
}
return 0;
}
题目:
描述
What is possibility of rolling N dice and the sum of the numbers equals to M?
输入
Two integers N and M. (1 ≤ N ≤ 100, 1 ≤ M ≤ 600)
输出
Output the possibility in percentage with 2 decimal places.样例输入
2 10
样例输出
8.33
这道题比较简单,使用模拟的方法,即可解决。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int maxn = 605;
const int maxm = 102;
const double min_val = 1e-9;
int n,m;
double mp[maxm][maxn];
int main(){
freopen("in.txt", "r", stdin);
int i,j,k, cnt;
while(scanf("%d %d", &n, &m) != EOF){
if(n > m || 6*n < m){
printf("%.2f\n", 0.00);
continue;
}
memset(mp, 0, sizeof(mp));
mp[0][0] = 1.0; cnt = 0;
for(i=1; i<=n; i++){
for(j=cnt; j<=6*cnt; j++){
for(k=1; k<=6; k++){
mp[i][j+k] += mp[i-1][j]*(1.0/6.0);
}
}
cnt++;
}
printf("%.2f\n", mp
[m]*100);
}
return 0;
}
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- C#递归算法之分而治之策略
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- C#算法之大牛生小牛的问题高效解决方法
- C#算法函数:获取一个字符串中的最大长度的数字
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- 经典排序算法之冒泡排序(Bubble sort)代码
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法