Re: [问题] 扔n次骰子,各种点数和的机率

楼主: conan0914 (è Ÿç­†)   2015-01-19 10:04:23
试着写写看 语法没有很在意
void GetPossibilityOfThrowDice (int numberOfDice)
{
// We want to get resultArray from 1 to 6N
int *resultArray = new int[6*numberOfDice+1];
SubFunction (0, numberOfDice, resultArray);
for(int i=0; i<=numberOfDice*6; i++)
{
printf("%d with possibility %f\n", i, (float)resultArray[i]/6*numberOfDice);
}
}
void SubFunction (int subSum, int remainingDice, int* resultArray)
{
// End Condition
if (remainingDice == 1)
{
for (int i=1; i<=6; ++i)
{
resultArray[subSum+i] += 1;
}
}
for (int i=1; i<=6; ++i)
{
SubFunction (subSum+i, remainingDice-1, resultArray);
}
}
楼主: conan0914 (è Ÿç­†)   2014-01-19 10:04:00
命名好讨厌>"<
作者: uranusjr (←這人是超級笨蛋)   2015-01-19 10:49:00
C# 风
作者: MOONRAKER (㊣牛鹤鳗毛人)   2015-01-19 11:02:00
WAY TOO LONG
作者: o07608 (无良记者)   2015-01-19 12:55:00
喔喔我参考看看0.0这样好像会造成跑不完的递回丢20次骰子,程式就跑不动了,可是这是功课要求的(喷笑)
作者: TobyH4cker (Toby (我要当好人))   2015-01-19 18:30:00
递回只要层数一多一定会很慢
作者: o07608 (无良记者)   2015-01-19 18:41:00
是阿,吃完饭回来写个20层for循环跑跑看
作者: holydc (のヮの)   2015-01-19 20:03:00
20 层要求多少时间啊??
作者: o07608 (无良记者)   2015-01-19 20:06:00
没有限定要跑多久,只要结果
作者: holydc (のヮの)   2015-01-19 21:05:00
不过不管怎样暴力法应该跑不完吧?? XD
作者: o07608 (无良记者)   2015-01-19 21:05:00
我是还没真的写20层循环下去......
作者: bigpigbigpig (To littlepig with love)   2015-01-19 21:17:00
怎么可能真的写20层循环,用递回写 DP 一下就算完了
作者: holydc (のヮの)   2015-01-19 22:45:00
看好久总算看出规则... http://ideone.com/WTJCCl

Links booklink

Contact Us: admin [ a t ] ucptt.com