简体   繁体   English

生成随机运动游戏结果C ++

[英]Generate random sport game result c++

I was required to write a program that generate random results of 10 sport games where 10 countries are involved, and display the medal tally based on the results. 我被要求编写一个程序,以生成涉及10个国家/地区的10个体育比赛的随机结果,并根据结果显示奖牌总数。

I was able to generate the games' result but have no idea how to sum up the results (ie the number of different medals earned by each country). 我能够生成游戏的结果,但不知道如何总结结果(即每个国家获得的不同奖牌的数量)。

Below is part of my code to generate random game result. 以下是我生成随机游戏结果的代码的一部分。

const string ctry[] = {'A','B','C','D','E','F','G','H','I','J'};  //country name

int main()
{
    string gctry[10];   //gold
    string sctry[10];   //silver
    string bctry[10];   //bronze

    for (int i = 0; i < 10; i++)
    {
        gctry[i] = country[(rand() + time(0))%10];
        sctry[i] = country[(rand() + time(0))%10];
        bctry[i] = country[(rand() + time(0))%10];
    }
}

I need some advice to solve this. 我需要一些建议来解决这个问题。 Thanks. 谢谢。

You will need to go through the medal arrays and determine how many instances of each country there is in each array, so if you loop through the gold array. 您将需要遍历奖牌阵列并确定每个阵列中每个国家有多少个实例,因此如果您遍历金牌阵列。

How you do this is up to you. 如何执行此操作取决于您。

Example if you loop through the GoldArray and find 3 instances of country "B" then they have 3 gold medals. 例如,如果您遍历GoldArray并找到3个国家“ B”的实例,那么它们就有3枚金牌。 You could provide a simple solution where you have a counter for each country. 您可以提供一个简单的解决方案,其中每个国家都有一个柜台。

int CountryAGoldM;

The each time you hit a country "A" in the array, you increment the CountryAGoldM by 1, I assume this is homework. 每次您击中数组中的国家“ A”时,都会将CountryAGoldM加1,我认为这是家庭作业。

In future you will see other ways to rewrite this to be more efficiant but I assume you are just learning so a simple solution is always best in this case :P 将来,您将看到其他方法来更有效地重写它,但我认为您只是在学习,因此在这种情况下,简单的解决方案始终是最好的:P

You don't need to add time(0) to each rand() result. 您无需在每个rand()结果中添加time(0)。 Instead use srand(time(0)) . 而是使用srand(time(0))

Your approach allows the same country to get more than one medal. 您的方法可以使同一国家获得多个奖牌。

You can use a std::map to count the medals. 您可以使用std :: map来计数奖牌。 No need to store any intermediate result in gctry, sctry and bctry. 无需将任何中间结果存储在gctry,sctry和bctry中。

您可以为此使用std :: count(),同时遍历各个国家/地区(尽管我更喜欢使用stl容器):

std::cout << country << ": gold " << std::count(gctry, gctry+10, country);

I know I shouldn't have done that, but sometimes you can learn by reading code ;) 我知道我不应该这样做,但是有时候您可以通过阅读代码来学习;)

#include <iostream>
#include <string>
#include <algorithm>
#include <map>
using namespace std;

int main()
{   
    const int nCountries = 7;
    string countries[nCountries] = { "C1", "C2", "C3", "C4", "C5", "C6", "C7" };
    map<string, int> goldMedals, silverMedals, bronzeMedals;

    const int nTrials = 10;

    for (int i = 0; i < nTrials; ++i)
    {
        // generate random results for current trial
        random_shuffle(countries, countries + nCountries);
        ++goldMedals  [countries[0]];
        ++silverMedals[countries[1]];
        ++bronzeMedals[countries[2]];

        //copy(countries, countries + nCountries, ostream_iterator<string>(cout, "\n"));
        //cout << "====================" << endl;
    }

    // sort by medals
    const int medalMultiplier = nTrials + 1;
    auto medalValue = [&](const string& c) 
    { 
        return goldMedals[c] * medalMultiplier * 2 + silverMedals[c] * medalMultiplier + bronzeMedals[c];
    };

    sort(countries, countries + nCountries, [&](const string& c1, const string& c2) { return medalValue(c1) > medalValue(c2); } );

    // output
    cout << "Results: \n";
    for (auto c = countries;  c != countries + nCountries; ++c)
    {
        cout << *c << " g" << goldMedals[*c] << " s" << silverMedals[*c] << " b" << bronzeMedals[*c] << endl;
    }
}

You have two problems to solve here. 您有两个要解决的问题。 One of them you didn't mention: 您没有提到其中一个:

Problem 1 问题1

You need to display sums of the number of medals won. 您需要显示获得的奖牌总数。 These sums will be grouped up by country, and by type of medal, but will not be grouped by game. 这些金额将按国家/地区和奖牌类型分组,但不会按游戏分组。

You already have created a list of countries, and three arrays, one for each medal type. 您已经创建了国家列表和三个阵列,每种奖牌类型一个。 The index of those arrays is the game, and the value of those arrays is the country that won the medal in question. 这些数组的索引是游戏,而这些数组的值是赢得相关奖牌的国家/地区。

Solution 1 解决方案1

One way to solve this would be to go through two nested loops. 解决此问题的一种方法是遍历两个嵌套循环。

For each country, start gold, silver, and bronze medal counters (inside the country loop). 对于每个国家/地区,在国家/地区循环中启动金牌,银牌和铜牌的计数器。

Then, for each game, check if they were the winners of the gold, silver, or bronze, and if so, add one to the corresponding count. 然后,对于每场比赛,检查他们是否是金牌,银牌或铜牌的获胜者,如果是,则将其加到相应的计数中。 Once you're done with all the games, print out how many medals of each type that the current country won. 完成所有游戏后,请打印出当前国家/地区赢得的每种类型的奖牌数。

Then continue with the next country. 然后继续下一个国家。

Problem 2 问题2

Can a country win two different medals in one game? 一个国家能否在一场比赛中赢得两枚不同的奖牌? If not, how would you solve this? 如果没有,您将如何解决?

Solution 2 解决方案2

Since you didn't directly ask this question, I won't put a solution yet. 由于您没有直接提出这个问题,所以我不会提出解决方案。 But you could look to how you would generate a unique combinations (ala your probability math classes), and just pick a random combination. 但是您可以考虑如何生成唯一的组合(例如概率数学类),然后选择一个随机组合。 You'd want to do 10 pick 3. 您想做10选3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM