C++赌徒硬币游戏 求解答

Rainboww_zhang 2017-10-07 07:10:26
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++求解救。
...全文
351 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
srhouyu 2017-10-07
  • 打赏
  • 举报
回复
掷一次硬币不需要什么先决条件,不需要参数。 它的效果就是类似于随机数发生器,每次调用随机返回一个true或者false

// 返回true表示正面,返回false表示反面
bool tosscoin()
{
    int result = rand() % 2;
    return (result == 1); // result==1的计算结果就是true或者false
}
为了表示1或者2胜,你可以返回1或者2,也可以返回1或者-1,也可以返回true或者false,等等。 我觉得返回true或者false比较方便。

// 返回true表示1胜,返回false表示2胜
bool gamlersruin(int n1, int n2)
{
    while( n1!=0 && n2 !=0)
    {
        if ( tosscoin() )
            n1 --;
        else
            n2 --;
    }
    return (n2==0); // 若n2==0,那么必然是1胜
}
Rainboww_zhang 2017-10-07
  • 打赏
  • 举报
回复
引用 1 楼 srhouyu的回复:
掷一次硬币不需要什么先决条件,不需要参数。 它的效果就是类似于随机数发生器,每次调用随机返回一个true或者false

// 返回true表示正面,返回false表示反面
bool tosscoin()
{
    int result = rand() % 2;
    return (result == 1); // result==1的计算结果就是true或者false
}
为了表示1或者2胜,你可以返回1或者2,也可以返回1或者-1,也可以返回true或者false,等等。 我觉得返回true或者false比较方便。

// 返回true表示1胜,返回false表示2胜
bool gamlersruin(int n1, int n2)
{
    while( n1!=0 && n2 !=0)
    {
        if ( tosscoin() )
            n1 --;
        else
            n2 --;
    }
    return (n2==0); // 若n2==0,那么必然是1胜
}
兄弟 谢了。时差问题回复不及时, 谢了。

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧