如何使用 c++ 对数字进行分组
How to group like numbers using c++
所以我写了一个程序来模拟10,000次掷骰子的总和并输出结果。现在我需要将所有重复的数字组合在一起,看看每个数字被掷出多少次。
for (int totalrolls = 0; totalrolls < 10000; totalrolls + 1) {
int dice1 = rand() % 6 + 1;//1st roll
int dice2 = rand() % 6 + 1;//2nd roll
int sumtotal = dice1 + dice2;//Sum of the two dice
cout << "Sum of dice rolls " << sumtotal << endl;
}
这是我为第一部分编写的代码。 数学并不是我的强项,所以我会感谢任何可以帮助我解决这个问题的方法。
编辑:我相信这会让你哭泣,但我对数组很废话,所以我尝试了别的东西。
#include "stdafx.h"
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
srand(time(NULL)); //Required to make the dice rolls random.
int start; //Variable to start the program
int dice1 = rand() % 6 + 1;//1st roll
int dice2 = rand() % 6 + 1;//2nd roll
int sumtotal = dice1 + dice2;//Sum of the two dice
int two = 0;
int three = 0;
int four = 0;
int five = 0;
int six = 0;
int seven = 0;
int eight = 0;
int nine = 0;
int ten = 0;
int eleven = 0;
int twelve = 0;
cout << "Write start to roll the dice: ";
cin >> start;
for (int totalrolls = 0; totalrolls < 10000; ++totalrolls) {
int dice1 = rand() % 6 + 1;//1st roll
int dice2 = rand() % 6 + 1;//2nd roll
int sumtotal = dice1 + dice2;//Sum of the two dice
cout << "Sum of dice rolls " << sumtotal << endl;
}
if (sumtotal == 2) {
two++;
}
if (sumtotal == 3) {
three++;
}
if (sumtotal == 4) {
four++;
}
if (sumtotal == 5) {
five++;
}
if (sumtotal == 6) {
six++;
}
if (sumtotal == 7) {
seven++;
}
if (sumtotal == 8) {
eight++;
}
if (sumtotal == 9) {
nine++;
}
if (sumtotal == 10) {
ten++;
}
if (sumtotal == 11) {
eleven++;
}
if (sumtotal == 12) {
twelve++;
}
cout << "2: " << two << endl;
cout << "3: " << three << endl;
cout << "4: " << four << endl;
cout << "5: " << five << endl;
cout << "6: " << six << endl;
cout << "7: " << seven << endl;
cout << "8: " << eight << endl;
cout << "9: " << nine << endl;
cout << "10: " << ten << endl;
cout << "11: " << eleven << endl;
cout << "12: " << twelve << endl;
return 0;
}
现在我意识到现在这有多混乱,但我正在有限的时间范围内工作。无论如何,它现在似乎非常接近工作,但它仍然不会记录每个数字有多少被滚动。(见图) https://i.stack.imgur.com/eZtVE.jpg
不要存储单个结果,定义unsigned occurences[12] = { 0 };
,当骰子输出n1
和n2
时,执行occurences[n1+n2-1] += 1
。
然后,您可以对所有内容求和:
unsigned long long total = 0;
for (int i = 1 ; i <= sizeof(occurences)/sizeof(unsigned) ; ++i) {
total += i * occurences[i-1];
}
现在我需要将所有重复的数字组合在一起,看看每个数字被掷出多少次。
这样,您问题的解决方案就微不足道了。
因此,您需要构建骰子的分布,即从骰子数量映射到出现次数。为此,任何关联(std::map, std::unordered_map)容器都可以。容器使用逻辑如下:
size_t diceVal = doThrowDice();
++container[diceVal];
更新:由于可能的骰子值在小域中,因此可以使用数组而不是关联容器。
const int maxDiceVal = 6;
int distribution[maxDiceVal];
memset(distribution,0, sizeof(distribution));
size_t diceVal = doThrowDice();
++distribution[diceVal];
此答案仅对您想要过滤那些相同的掷骰子的假设有效。
// Stores the grouping of equal dice rolls where dice1==dice2
std::vector<int> group(7,0);
for (int totalrolls = 0; totalrolls < 10000; totalrolls++) {
int dice1 = rand() % 6 + 1;//1st roll
int dice2 = rand() % 6 + 1;//2nd roll
int sumtotal = dice1 + dice2;//Sum of the two dice
std::cout << "Sum of dice rolls " << sumtotal << std::endl;
if (dice1==dice2)
{
group[dice1] += 1;
}
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 使用if-else将数字转换为单词
- 使用fstream对txt文件中的数字进行循环
- 我需要知道如何在 c++ 中使用增量值添加到一定数字
- uint_not_usable_without_attribute在业力规则中使用数字生成器时静态断言失败
- 使用 BMI 计算器对C++中的数字进行四舍五入的问题
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 函数如何使用引用返回所需的数字?
- 使用浮点数和双精度数的非常小数字的数学
- 使用 cin 时接受小数点后的 2 位数字
- 使用设置精度时如何阻止数字向上舍入?
- scanf() 只取数字作为输入,避免使用其他字符
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- 如何在C++程序中使用下标数字?
- 为什么使用数组元素查找最大数字的程序不起作用?
- 字符串中的数字总和(使用字符串对象)C++
- c++ rand()每个数字只使用一次
- 数字签名使用证书和密钥从USB令牌
- 解析(浮点)数字时使用了哪些信息