Rainboww_zhang 2017-10-06 23:17 采纳率: 0%
浏览 925

C++赌徒硬币游戏 大二物理专业留学党求救 真的是不会了 刚学两周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++求解救。

  • 写回答

1条回答 默认 最新

  • 四夕丶 2017-10-08 07:50
    关注

    2333,楼主像一个惊慌失措的小孩子。
    一个函数,除非他是void型的,其他函数总要有返回值的,返回值就是函数运行完后的结果,用语句return什么什么 来表示。
    就这道题来说,正面返回true,反面返回false,那么代码就应该是(按你写的来)
    if (result==1)
    retrurn true;
    else
    return false;
    那么函数结束后,函数就变成了“true”或“false”,比如 bool i=tosscoin(参数),那么这个i可能是true也可能是false,总之,这个函数的返回值
    赋值给了i.
    但是,一旦函数执行到return 语句的时候,无论下面还有没有语句,函数都会结束.
    你的函数没有return语句,而且,传进去的两个参数是没有必要的。
    正确来说(一种实现方法,你也可以用别的方法),应该不传任何参数进去,函数体里面只有判断并返回的语句
    bool tosscoin()
    {
    int result = rand() % 2;
    if (result==1)
    cout<<"Heads\n";
    else
    cout<<"Tails\n";
    }
    至于第二题,无非一个循环,每抛一次就用tosscoin函数判断一次正反,根据正反决定谁的硬币+1,谁的-1;

    从你的代码里可以看出来你很多知识不知道,比如bool tosscoin(int i,int result)你传进来了i和result,在函数里你仍然从新定义了一遍。
    要多看看书了。

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料