【问题标题】:Roll 2 dice 1000 times掷 2 个骰子 1000 次
【发布时间】:2018-12-17 19:16:30
【问题描述】:

我的教授要求我们编写一个程序:

  1. 使用循环模拟一对骰子滚动一千次(这里我认为 for 循环会很有用)。

  2. 每次迭代时,循环需要计算每个值从 2 到 12 的次数(这里我认为 if/else 语句会适用)

  3. 当循环结束时,必须显示每个值(从 2 到 12)出现的次数。

他的作业结构如下:

他希望我们使用一个进入 1000 次 for 循环的函数,每个函数调用两次调用另一个函数(模拟掷两个骰子)。

让我解释一下我设法放下了什么

//
//  main.cpp
//  RollingDice

#include <iostream>
#include <ctime>
using namespace std;
int roll();
int rollDice();

int main(int argc, const char * argv[])
{

    for (int i = 1; i < 1000; i++)
    {

        rollDice(); //This is the function that is supposed to call the roll(); 
                    //function two times. This makes sense to me that TWO DICE 
                    //are being rolled 1000 times. 

    }

    int result; //These two statements was where I was just hoping for something 
                //to work. I put these variable statements outside of the for 
                //loop because I was thinking that the int rollDice(); function 
                //definition(below) wouldn't know how to use it otherwise. I 
                //know that doesn't make sense, but I just can't explain why. 

    result = rollDice();

}

int roll()
{ //This function was provided to us by my professor. 
    static bool randomInitialized = false;

    int points;

    if (!randomInitialized)
    {
        srand((unsigned int) time(NULL));
        randomInitialized = true;

    }
    points = (rand() % 6) + 1;
    return points;
}

int rollDice()
{ //This I wrote myself. I'm imagining this is how you call a function twice. 
  //The return statement in this function was my attempt of returning the sum
  //of the values of the two dice.
    roll();
    roll();

    return result;
}

除了程序的这一部分不起作用之外,我仍然遇到的另一个问题是确定一种方法来为每个发生的值设置一个计数器(但是,我想象程序的该部分属于 for 循环。那就是我所知道的一切。)。从昨天开始,我就对这个计划进行了深入的思考。我今天回到它希望一个新的头脑能解决它,但我仍然在挣扎。非常感谢任何和所有帮助。

【问题讨论】:

  • 与其说所有randomInitialized rigamarole,不如在main 的第一行调用srand
  • 计数器可以是一个 int 数组,只增加数组[result]
  • @FeiXiang 评论补充:学习使用数组。询问教授是否对使用数组有任何限制。遵循分配规则并获得好成绩,但要知道没有理智的程序员会在这里使用if/else if。如果不允许使用数组,请询问switch/caseif/else if应该是最后的手段。
  • @RetiredNinja 一个合理但无聊的故事。我也接受“因为正确使用srand/rand 是未来的课程。”我很可能会称赞“因为现在是 2018 年,srand/rand 很愚蠢,所以我将在接下来的几周内教 &lt;random&gt; 库。”
  • @McMissile srand(time(0)); 不会“随机播种函数”。多次调用srand 会使事情变得更糟:很可能整个for 循环将在几分之一秒内运行,所以如果你在每次rand() 调用之间调用srand(time(0)),你可以得到相同的结果rand() 全部 1000 次!

标签: c++ dice


【解决方案1】:

表达式roll() 计算为一个数字。要添加数字,我们使用+。要返回一个值,我们使用return

把它们放在一起,我们得到一个简单的函数来求和两个卷

int rollDice() { return roll() + roll(); }

如果您有一个编号的事物序列,并且数字都靠得很近并且从 0 附近开始,则标准库的 SequenceContainers 之一是整个序列的合适持有者。

这里的东西是特定投掷的计数。我们预先知道可用的值(包括 2 - 12),所以 std::array 是合适的。任何可以容纳至少 1000 的整数值都适用于计数。我这里选择std::size_t

#include <array>
#include <iostream>

int main()
{
    std::array<std::size_t, 13> counts {}; 

这将给我们 13 个 0,从位置 0 开始

    for (std::size_t i = 0; i < 1000; ++i)
    {
         ++counts[rollDice()]; 

我们用 rollDice 选择哪个数字,并用它来选择要增加的计数

    }

    for (std::size_t i = 2; i < 13; ++i)
    {

我们现在可以遍历我们的结果,显示计数

         std::cout << "The count for " << i << " is " << counts[i] << std::endl;
    }
}

【讨论】:

    【解决方案2】:

    1- 使用地图计算每个数字从 2 到 12 的次数:(最实用)

    int sumOfDice;
    map <int,int> diceOccurances;
    for (int i=0; i < 1000; i++)
    { 
        sumOfDice=rollDice();
        diceOccurances[sumOfDice];
        // Here you are storing how many times each of the dice values occured. Here's 
        // how you access the map;
    }
    for (auto const& x : socks)
    {
            cout <<" Dice Total Number: " << x.first ;
            cout <<" Dice Number of Occurances: "<< x.second<<endl;
    }
    int rollDice()
    { //This I wrote myself. I'm imagining this is how you call a function twice. 
      //The return statement in this function was my attempt of returning the sum
      //of the values of the two dice.
        int die1,die2;
        die1= roll();
        die2= roll();
        result = die1+die2;
        return result;
    }
    

    2- 使用 if/else(或 switch);

    int two, three, four, five, six, seven, eight, nine, ten ,eleven ,twelve;
    two=three=four=five=six=seven=eight=nine=ten=eleven=twelve=0;
    for (int i=0; i < 1000; i++)
        { 
            if ( rollDice()==2) two++;
            if (rollDice()==3) three++;
            if (rollDice()==4) four++;
            // and so forth until twelve++;
        }
    int rollDice()
        { //This I wrote myself. I'm imagining this is how you call a function twice. 
          //The return statement in this function was my attempt of returning the sum
          //of the values of the two dice.
            int die1,die2;
            die1= roll();
            die2= roll();
            result = die1+die2;
            return result;
        }
    

    【讨论】:

      【解决方案3】:

      你可以做这样的事情(需要一些改动以适应你的 Proff 代码的其余部分)

      int rollDice()
      
      int main(int argc, const char * argv[])
      {
        srand(time(0));// randomly seed every time you run the code
        int dice1;
        int dice2;
        int storageData[11];//stores the number of times the values from 2 to 12   appears;
        for(int i=0; i<1000; i++)
        {
          dice1=rollDice();
          dice2=rollDice();
          int sum=dice1+dice2;
          storageData[sum-2]+=1;  // updates the number of times the sum has appeared.
      
        }
      cout << "the value x has appeared "<< storageData[x-2] <<" times"<< endl; // change the value of x depending on which sum number you want.
      }
      
      int rollDice()
      {
        int x=rand()%6+1;// rand()%6 produces a no. from 0-5. rand()%6+1 produces a number from 1-6.
        return x;
      }
      

      注意,在上面的代码中,我们为每个元素减去 (-2),因为总和从 2 开始,而不是 0。

      【讨论】:

        猜你喜欢
        • 2020-06-06
        • 2016-02-07
        • 2020-04-29
        • 1970-01-01
        • 2021-02-23
        • 1970-01-01
        • 1970-01-01
        • 2017-02-21
        • 1970-01-01
        相关资源
        最近更新 更多