书鸢1236 2023-01-07 00:21 采纳率: 66.7%
浏览 288
已结题

类与抽象中掷骰子问题

题目详情:掷骰子 - C/C++ 类与抽象

img

重要说明:由于本题代码涉及到随机数,因此出题者无法设定固定的答案来评测答题者的程序。所以,出题者在后台设定了一个不同于题面的测试程序,该程序将会观察Dice类所得到的各种数据的一致性及随机性,如果正确,输出Done。

对于答题者而言,请依据上述样例进行本地测试,使得输出结果与下述样例类似即可(具体数值除外):
进一步说明:


```c++
属性int iSides,表示骰子的面数,值固定为6;
函数int rollCount()返回骰子被摇的总次数;
函数int sideCount(int s)返回s点被摇中的总次数,其中,s取值范围为1 ~ 6;
函数int rollDice()返回一个值为1 ~ 6的随机点数;

//题目样例答案

```c++
-------Roll dice for 1000 times------
3, 4, 5, 2, 6, 2, 2, 6, 5, 1, ...
------Statistics of rolling the  dice------
Side 1: 158 / 1000 = 15.8%.
Side 2: 156 / 1000 = 15.6%.
Side 3: 161 / 1000 = 16.1%.
Side 4: 175 / 1000 = 17.5%.
Side 5: 165 / 1000 = 16.5%.
Side 6: 185 / 1000 = 18.5%.

我的解答思路和尝试过的方法
首先按照要求先定义Date类里面的成员

class Dice {
public:
    static int iSides = 6;
    int rollCount();
    int sideCount(int s);
    int rollDice();
};

然后:
根据原有代码中间

auto rc = d.rollCount();
cout << "Side " << i << ": " << c << " / " << rc
             << " = " << 100.0*c/rc << "%.\n";中间的   100.0*c/rc 部分
以及样例答案Side 6: 185 / 1000 = 18.5%.

推测 rc应该是1000;
那么rollCount();的返回值应该为1000
从而写出

int Dice::rollCount() {
    return 1000;
}

的结果
再根据题目要求

函数int sideCount(int s)返回s点被摇中的总次数,其中,s取值范围为1 ~ 6

从而写出

int Dice::rollDice() {
    int N = 1 + rand() % 6;
    return N;
}

最后是根据题目要求
函数int rollCount()返回骰子被摇的总次数;

int Dice::sideCount(int s) {
    int n = 0;
    for (int i = 0; i <= rollCount(); i++) {
        if (rollDice() == s)
            n++;
    }
    return n;
}

以下是我代码部分以及运行结果


class Dice {
public:
    static int iSides;
    int rollCount();
    int sideCount(int s);
    int rollDice();
};
int Dice::iSides = 6;
int Dice::rollDice() {
    int N = 1 + rand() % 6;
    return N;
}

int Dice::sideCount(int s) {
    int n = 0;
    for (int i = 0; i <= rollCount(); i++) {
        if (rollDice() == s)
            n++;
    }
    return n;
}

int Dice::rollCount() {
    return 1000;
}

img

但是答案错误

  • 写回答

2条回答 默认 最新

  • a5156520 2023-01-07 10:57
    关注

    在sideCount()函数中增加一个srand(0),即重置随机数种子为0,然后把函数中的i <= rollCount()改为i < rollCount(),即产生1000次随机数,输出和样例就一致了,修改如下:

    参考链接:

     #include <iostream>
     #include <cstdlib>
     using namespace std;
    class Dice {
    public:
    // http://c.biancheng.net/view/2221.html
        Dice();
        static int iSides;
        int rollCount();
        int sideCount(int s);
        int rollDice();
    };
    int Dice::iSides = 6;
    int Dice::rollDice() {
        int N = 1 + rand() % 6;
        return N;
    }
     
     
    int Dice::sideCount(int s) {
        int n = 0;
        srand(0); // 重置随机数种子 
        // 产生1000次随机数 
        for (int i = 0; i < rollCount(); i++) {
            if (rollDice() == s)
                n++;
        }
        return n;
    }
     
     Dice::Dice(){
         
     }
     
    int Dice::rollCount() {
        return 1000;
    }
     
     int  main(){
         
         srand(0);
         Dice  d = Dice();
         
         cout<<"--------Roll dice for 1000 times---------\n";
         for(int  i=0;i<1000;i++){
             int  r = d.rollDice();
             if(i<10)
                 cout<<r<<", ";
         }
         cout<<"...\n";
         
         
         cout<<"--------Statistics of rolling the dice----------\n";
         for(int i=1;i<=d.iSides;i++){
             int c = d.sideCount(i);
             int  rc = d.rollCount();
             cout<<"Side "<<i<<": "<<c<<" / "<<rc
             <<" = "<<100.0*c/rc << "%.\n";
         }
         
         return 0;
     }
    
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月14日
  • 已采纳回答 1月7日
  • 创建了问题 1月7日

悬赏问题

  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因