如何使用 c++ 对数字进行分组

How to group like numbers using c++

本文关键字:数字 何使用 c++      更新时间:2023-10-16

所以我写了一个程序来模拟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 };,当骰子输出n1n2时,执行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;
    }
}