程序可以滚动2个骰子36000次 我有一个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. */
我创建了这个程序,但它的输出如下:程序可以滚动2个骰子36000次 我有一个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. */,c++,arrays,dice,C++,Arrays,Dice,我创建了这个程序,但它的输出如下: sum of Faces Frequency 2 0 3 4041 4 0 5 7922 6 0 7 12154 8 0 9 7936 10 0 11 3
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;
}
您似乎不是在播种随机函数,这可能会产生奇怪的结果 解决方案:
#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]++;
}
#包括//时间()
#包括//srand(),rand()
...
srand(时间(空));//有更好的方法为rand()设定种子,但这很简单
...
对于(int roll1=0;roll1我只是复制您的代码并运行它,您似乎缺少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;
}
#包括
使用名称空间std;
int main()
{
常数int arraysize=13;
整数计数器[13],和=0;
//初始化计数器
对于(inti=0;i让我们玩骰子吧!传统的骰子是一个立方体。它的六个面中的每一个都显示了从1到6的不同点数。骰子用于产生从1到6的结果。当掷骰子(或掷骰子)时当模具静止时,模具最上面的面提供了掷骰的值。如果掷骰没有偏差,则从1到6的每个值的可能性相等
您的编程任务与滚动模具产生的序列分析相关
创建一个名为Dice的程序来解决以下练习。该程序的输入是所谓的试用,即滚动结果的序列。该输入必须从标准输入中读取。输入的第一行包含一个整数N,表示抛出次数\left(1\len:\le1000000\right)。输入的第二行正好包含N个字符,每个字符是1到6之间的数字。例如,输入可以如下所示:
八,
32646135
程序的输出应写入标准输出。您需要解决3个练习。输出应正好包含3行:输出中的第i行是练习i的解决方案
练习
练习1
在试验中发生了多少次,恰好两个6在后面滚动?例如,在序列5661116626634416中,发生了两次,正好两个6在后面滚动
练习2
查找连续滚动的最长子序列的长度,其中未出现值6。(如果仅抛出6秒,则该数字可以为零。)例如,在试验66423612345654中,连续滚动的最长子序列中未出现值6的是12345。其长度为5
练习3
如果试验中的连续滚动序列仅包含5s和6s,我们将其称为幸运序列。例如,6556665是一个幸运序列,长度为7。
找出幸运系列最频繁的长度。如果有多个“最频繁”的幸运系列长度,则打印最长的长度。如果试用中没有幸运系列,则打印零
请注意。我们对最常见的幸运系列不感兴趣。四个幸运系列656、555、556和666对我们来说是等效的,它们都是长度为3的幸运系列。我们正在寻找最常见的幸运系列长度
例如,在试验5533661656中,656系列是最长的幸运系列。但试验中只有一个长度为3的幸运系列。55和66也是幸运系列。这就是为什么正确答案是2。在试验4561613656124566中,长度为2和3的幸运系列出现两次,它们之间存在平局。现在,长度为最长的(即3)应该打印出来。示例1和示例2旨在说明这种情况
例子
我们提供三个简单的例子来说明练习。用其他更大的例子来测试你的解决方案
例1
样本输入:
九,
616161666
样本输出:
0
1.
1.
例2
样本输入:
十八
456116513656124566
样本输出:
一,
4.
3.
例3
样本输入:
十七,
56611166626634416
样本输出:
二,
4.
3又来了一个作业,让我们来做吧!!:D我不知道为什么它给所有偶数赋予0频率
为什么不?你写了程序吗?如果你写了,调试你的程序以发现偶数行为不正确的原因。看起来你的rand
的实现可能很差。尝试使用现代的库。如果您没有复制并粘贴该代码,而是重新键入它,我会查找一个打字错误,其中die1
意外变成die2
,反之亦然。@moose您可以非常轻松地成为自己的侦探。只需添加两行简单的cout
即可输出您正在使用的值(最基本的调试技术——输出值)。如果您只输出die1
和die2
的值,那么您的问题将集中到rand()
函数无法正常工作。那么您的问题可能是my rand()当我执行此操作时,函数无法正常工作…
而不是我不知道我的程序出了什么问题
。不进行种子设定不会给出“奇怪”结果;它只会为每个程序运行提供相同的结果。如果实现被充分破坏,默认种子永远不会给出两个连续的偶数,那么种子设定将不会有帮助。@levi我们不允许使用ctime或cstdlib。您有没有不涉及额外LIB的解决方案?可能性又如何ies?@moose您正在使用的rand()实现已损坏,而不是您的程序。@moose:如果您不能使用
,那么您就不能使用rand()
(当您包含其他库头时,您的程序一定是意外地包含了它)。如果你想编写自己的伪随机数生成器,那么谷歌会很快告诉你怎么做。这是完全相同的代码,但有一个非标准的头。@yahyalfakir
#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;
}