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

时间:2016-02-26 13:31:20

标签: c++ math random

所以我写了一个程序来模拟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;
}

现在我意识到现在有多乱,但我正在限时制作。无论如何它现在似乎非常接近工作但它仍然不会记录每个数字的滚动数量。 (见图)http://imgur.com/zLNfgSy

3 个答案:

答案 0 :(得分:1)

不要存储单个结果,定义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];
}
  

现在我需要将所有重复的数字组合在一起,看看每个数字的滚动次数。

这样,解决问题的方法很简单。

答案 1 :(得分: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];

答案 2 :(得分:1)

这个答案仅适用于你想要过滤那些完全相同的骰子的假设。

// 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;
    }
}