程序可以掷 2 个骰子 36000 次
Program to roll 2 dice 36000 times with possibilities
我有一个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
您似乎没有为随机函数播种,这会产生奇怪的结果
解决方案:
#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),check this out
我只是复制了你的代码并 运行 它,你似乎缺少 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;
}
在我的机器上 运行ning 正确。
来玩骰子吧!传统的骰子是一个立方体。它的六个面中的每一个都显示从 1 到 6 的不同数量的点。骰子用于产生从 1 到 6 的结果。当掷出(或滚动)骰子并且骰子停止时,骰子的面是uppermost 提供抛出的值。如果掷出无偏骰子,则从 1 到 6 的每个值出现的可能性均等。
您的编程任务与分析掷骰子产生的序列有关。
创建一个名为 Dice 的程序来解决以下练习。该程序的输入是所谓的试验,即滚动结果的序列。此输入必须从标准输入中读取。输入的第一行包含一个整数N,表示抛出的次数\left(1\le N:\le1000000\right)。输入的第二行恰好有N个字符,每个字符是1到6的数字。例如,输入可以如下:
8
32646135
程序的输出应该写入标准输出。您需要解决 3 个练习。输出应恰好包含 3 行:输出中的第 i 行是练习 i.
的解决方案
练习
练习 1
试验中出现了多少次恰好两个 6 依次掷出?例如,在序列 56611166626634416 中出现了两次,恰好有两个 6 被抛出。
练习 2
找出连续滚动的最长子序列的长度,其中不出现值 6。 (如果只抛出 6,这个数字可以为零。)例如,在试验 66423612345654 中,连续掷出值 6 不出现的最长子序列是 12345。它的长度是 5.
练习 3
如果序列只包含 5 和 6,我们将把试验中的一系列连续掷骰称为幸运系列。例如 6556665 是一个幸运系列,长度为 7。
找出,这是幸运系列最常见的长度。如果有多个“最常见”的幸运系列长度,则打印最长的。如果试验中没有幸运系列,则打印零。
注意。我们对出现频率最高的幸运系列不感兴趣。幸运系列656、555、556、666这四个对我们来说是等价的,都是长度为三的幸运系列。我们正在寻找最频繁的幸运系列长度。
例如试炼5533661656,656系列是最长的幸运系列。但是审判中只有一个长度为三的幸运系列。 55和66也是幸运系列。这就是为什么正确答案是2的原因。在试验456116513656124566中,长度为2和3的幸运序列都出现了两次,它们之间有平局。现在应该打印最长的长度(即 3)。例子example1和example2就是为了把这种情况说清楚。
例子
我们提供了三个简单的例子来阐明练习。还要用其他更大的示例测试您的解决方案!
示例 1
示例输入:
9
616161666
示例输出:
0
1个
1个
示例 2
示例输入:
18
456116513656124566
示例输出:
1
4个
3个
示例 3
示例输入:
17
56611166626634416
示例输出:
2
4个
3
我有一个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
您似乎没有为随机函数播种,这会产生奇怪的结果
解决方案:
#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),check this out
我只是复制了你的代码并 运行 它,你似乎缺少 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;
}
在我的机器上 运行ning 正确。
来玩骰子吧!传统的骰子是一个立方体。它的六个面中的每一个都显示从 1 到 6 的不同数量的点。骰子用于产生从 1 到 6 的结果。当掷出(或滚动)骰子并且骰子停止时,骰子的面是uppermost 提供抛出的值。如果掷出无偏骰子,则从 1 到 6 的每个值出现的可能性均等。
您的编程任务与分析掷骰子产生的序列有关。
创建一个名为 Dice 的程序来解决以下练习。该程序的输入是所谓的试验,即滚动结果的序列。此输入必须从标准输入中读取。输入的第一行包含一个整数N,表示抛出的次数\left(1\le N:\le1000000\right)。输入的第二行恰好有N个字符,每个字符是1到6的数字。例如,输入可以如下:
8 32646135 程序的输出应该写入标准输出。您需要解决 3 个练习。输出应恰好包含 3 行:输出中的第 i 行是练习 i.
的解决方案练习 练习 1 试验中出现了多少次恰好两个 6 依次掷出?例如,在序列 56611166626634416 中出现了两次,恰好有两个 6 被抛出。
练习 2 找出连续滚动的最长子序列的长度,其中不出现值 6。 (如果只抛出 6,这个数字可以为零。)例如,在试验 66423612345654 中,连续掷出值 6 不出现的最长子序列是 12345。它的长度是 5.
练习 3 如果序列只包含 5 和 6,我们将把试验中的一系列连续掷骰称为幸运系列。例如 6556665 是一个幸运系列,长度为 7。 找出,这是幸运系列最常见的长度。如果有多个“最常见”的幸运系列长度,则打印最长的。如果试验中没有幸运系列,则打印零。
注意。我们对出现频率最高的幸运系列不感兴趣。幸运系列656、555、556、666这四个对我们来说是等价的,都是长度为三的幸运系列。我们正在寻找最频繁的幸运系列长度。
例如试炼5533661656,656系列是最长的幸运系列。但是审判中只有一个长度为三的幸运系列。 55和66也是幸运系列。这就是为什么正确答案是2的原因。在试验456116513656124566中,长度为2和3的幸运序列都出现了两次,它们之间有平局。现在应该打印最长的长度(即 3)。例子example1和example2就是为了把这种情况说清楚。
例子 我们提供了三个简单的例子来阐明练习。还要用其他更大的示例测试您的解决方案!
示例 1 示例输入:
9 616161666 示例输出:
0 1个 1个 示例 2 示例输入:
18 456116513656124566 示例输出:
1 4个 3个 示例 3 示例输入:
17 56611166626634416 示例输出:
2 4个 3