C++赌徒硬币游戏 求解答
Let two players each have a finite number of euro coins (say, n1 for player one and n2 for player two). Now, flip one of the euros (temporarily borrowed from one or other player), with each player having 50% probability of winning, and transfer a euro from the loser to the winner. Now repeat the process until one player wins and owns all the euros (and one of the players loses and has no euros left).
PART 1
Write a function in C++ to model the tossing of a fair coin with the following signature
bool tosscoin();
The function returns true for heads and false for tails.
PART 2
Write a function in C++ to implement a single run of the gambler’s ruin with the following signature:
bool gamlersruin(int n1, int n2);
Where n1 is the initial number of coins belonging to player 1 and n2 is the initial number of coins belonging to player 2. Make use of the tosscoin() function when implementing this function.
The function returns true if player 1 wins and false if player 2 wins.
PART 3
In main() write code which investigates the following situations:
(i) n1=n2=100. Do 100 simulations. Output the number of times player 1 wins and the number of times player 2 wins. (ii) n1=150, n2 = 50. Do 100 simulations. Output the number of times player 1 wins and the number of times player 2 wins.
大概意思是
有两个人分别有n1,n2个硬币,投另外一个硬币,每个人都有百分之50的几率赢,赢者就从输者那里拿走一个硬币。(判定规则在这里没写,老师说是 让两个赌徒分别选择硬币的正反面,比如玩家1选正面赢,那玩家2就是背面赢)。直到其中一人没有硬币了, 游戏结束,另一个人获胜。
问题1
用 bool 写一个函数来模拟投掷硬币这个事 函数名字是tosscoin。
正面返回值为true 背面为false
问题2
用bool写一个函数 完成一次游戏 函数名字是gamlersruin(int n1, int n2), n1和n2分别是两个玩家的硬币数量。(这个函数里要把问题1的tosscoin()用到。
问题3 我想自己想想。。。。问题2 我是真的不会了。。。
int main()
{
int result,i;
bool tosscoin(int i,int result);
cout << "How much times do you want to toss????? = " << endl;
cin>>i;
srand((unsigned int)time(0));
cout<<tosscoin(i,result)<<endl;
return 0;
}
bool tosscoin(int i,int result)
{
int n;
for(n=0;n<i;n++)
{
int result = rand() % 2;
if (result==1)
cout<<"Heads\n";
else
cout<<"Tails\n";
}
}
这是我自己写的问题1,感觉跟问题1的题目要求有点出入, 我不知道返回ture和false具体怎么使用。请大神们指教。
这是我第一次知道这个论坛并在这求救。本人大二物理专业留学党,刚刚学习C++求解救。