程序可以滚动2骰子36000次,并具有可能性

Program to roll 2 dice 36000 times with possibilities

本文关键字:可能性 36000次 骰子 滚动 程序      更新时间:2023-10-16

我有一个C 作业要做,这是问题

//Dietel & Dietel C Programming //Chapter 6 Arrays: Page 241 Exercise:
6.19 /* Write a program that simulates the rolling of two dice. 
* The program should use rand to roll the first die, and 
* should use rand again to roll the second die. 
* The sum of the two values should then be calculated. (Note: Since each die 
* can show an integer value from 1 to 6, then the sum of the two values will vary
* from 2 to 12 with 7 being the most freqent sum and 2 and 12 being the least frequent
* sums.) Figure 6.23 shows the 36 possible combinations of the two dice. 
* Your program should
* roll the two dice 36,000 times. Use a single-scripted array to tally the numbers of times 
* each possible sum appears. 
* Print the results in a tabular format. Also, determine if the totals 
* are resonable; i.e there are six ways to roll a 7, so approximately one sixth of all of the
* rolls should be 7. 
*/

我创建了程序,但它为我提供了这样的输出:

sum of Faces    Frequency
      2            0
      3         4041
      4            0
      5         7922
      6            0
      7        12154
      8            0
      9         7936
     10            0
     11         3948
     12            0
sum: 36001

我不为什么给所有偶数数字给0频率

这是我到目前为止编码的内容:

#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
    const int arraysize = 13;
    int counter[13], sum=0;
    // init counter
    for(int i=0; i<13; i++)
        counter[i] = 0;
    int die1; 
    int die2;
    for ( int roll1 = 0; roll1 <=36000; roll1++ ) {
        die1 =  1 + rand() % 6;
        die2 =  1 + rand() % 6;
        counter[die1+die2]++;
    }
    cout<<"sum of Faces"<<setw(13)<<"Frequency"<<endl;
    for(int face=2; face<arraysize;face++)
    {
        cout<<setw(7)<<face<<setw(13)<<counter[face]<<endl;
        sum += counter[face];
    }
    cout << "sum: " << sum;

    return 0;
}

我还需要增加骰子的可能性,例如:

1 + 1 = 2 : 1 possibility for sum to be 2
1 + 2 = 2 + 1 = 3 : 2 possibility for sum to be 3
1 + 3 = 2 + 2 = 3 + 1 = 4 : 3 possibility for sum to be 4
.
.
.
6 + 6 = 12 : 1 possibility for sum to be 12

我只是复制您的代码并运行它,您似乎缺少rand函数的库(不知道您是如何运行自己的),无论如何我刚刚导入rand库....

#include <bits/stdc++.h>
using namespace std;
int main()
{
    const int arraysize = 13;
    int counter[13], sum=0;
    // init counter
    for(int i=0; i<13; i++)
        counter[i] = 0;
    int die1; 
    int die2;
    for ( int roll1 = 0; roll1 <=36000; roll1++ ) {
        die1 =  1 + rand() % 6;
        die2 =  1 + rand() % 6;
        counter[die1+die2]++;
    }
    cout<<"sum of Faces"<<setw(13)<<"Frequency"<<endl;
    for(int face=2; face<arraysize;face++)
    {
        cout<<setw(7)<<face<<setw(13)<<counter[face]<<endl;
        sum += counter[face];
    }
    cout << "sum: " << sum;

    return 0;
}

它在我的计算机中正确运行。

您似乎不会播种随机功能,这会产生奇怪的结果

解决方案:

#include <ctime>   // time()
#include <cstdlib> // srand(), rand()
...
srand(time(NULL)); // There are better ways to seed rand(), but this is simple
...
for ( int roll1 = 0; roll1 <=36000; roll1++ ) {
    die1 =  1 + rand() % 6;
    die2 =  1 + rand() % 6;
    counter[die1+die2]++;
}

另外,如果您想要更多的C - 喜欢解决方案(并且可以访问C 11),请查看此

让我们玩骰子!传统的模具是立方体。它的六个面中的每一个都显示出不同数量的点从1到6。骰子用于产生1到6的结果。最高提供了投掷的价值。如果抛出公正的模具,则每个值从1到6都可能同样可能。

您的编程任务与滚动模具产生的序列分析有关。

创建一个名为DICE的程序来解决以下练习。该程序的输入是一个所谓的试验,即滚动结果的序列。该输入必须从标准输入中读取。输入的第一行包含一个单个整数n,指示throw的数量 left(1 le n: le1000000 right)。输入的第二行包含n个字符,每个字符都是1到6的数字。例如,输入可以如下:

832646135程序的输出应写入标准输出。您期望有3种练习解决。输出应完全包含3行:输出中的ITH线是练习I。

的解决方案。

练习练习1在试验中发生了多少次,彼此之间恰好有两个6次滚动?例如,按序列566111666666634416发生了两次,彼此之间正好抛出了两个6

练习2找到连续卷的最长子序列的长度,其中未发生值6。(如果只投掷6秒,这个数字可以为零。)例如,在试验中66423612345654未发生值6的连续卷的最长子序列是12345。其长度为5。

练习3如果序列仅包含5s和6s,我们将在试验中将连续的卷列为幸运的系列。例如655665是一个幸运的系列,长度为7。找出答案,这是Lucky系列最常见的长度。如果有一个以上的"最常见"幸运的系列长度,打印最长的时间。如果试验中没有幸运的系列,请打印零。

请注意。我们对最常见的幸运系列不感兴趣。四个幸运的656、555、556和666对我们来说相当,所有这些都是Lucky系列的三个长度。我们正在寻找幸运系列中最常见的长度。

例如,在试验5533661656中,该系列656是最长的幸运系列。但是在审判中只有一个幸运的Lenght系列。55和66也是幸运的系列。这就是为什么正确答案为2。在试验456116513656124566中,这两个幸运系列的长度为2和3,两次发生,它们之间有一条领带。现在,应打印最长的长度(即3)。示例示例1和示例2的目的是使这种情况清楚。


示例我们提供三个简单的例子来阐明练习。还用其他较大的示例测试您的解决方案!

示例1样本输入:

9616161666样本输出:

011示例2样本输入:

18456116513656124566样本输出:

143示例3样本输入:

17566111666666634416样本输出:

243