类与抽象中掷骰子问题

题目详情:掷骰子 - 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

但是答案错误

在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

添加两个类成员 ,一个是整形数组形式长度为6,代表各面摇出多少次,初始化为0.
执行rolldice一次,则相应的数组元素加+1
另一个是整形代表摇了多少次,初始化为0,执行rolldice 一次,相应加1