C团队中的球员组合 [英] combinations of players for a team in C

查看:100
本文介绍了C团队中的球员组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成所有的球员组合,以组成一个篮球运动员团队. 假设有5个位置(SG,PG,SF,PF,C),我需要用9名球员填补雄鸽,每个位置2个位置,除了中心位置(只有1个位置).

I am trying to generate all combinations of players to make up a team of basketball players. Let's say there's 5 positions(SG, PG, SF, PF, C) and I need to fill a rooster with 9 players, 2 of each position except the Center position for which there's only 1.

比方说,每个位置我有10个玩家,我如何生成所有可能排列的列表.

Let's say I have 10 players for each position, how can I generate a list of all possible permutations.

我想在一个csv文件中从excel导入名称,然后在另一个csv文件中将所有组合输出回excel.

I would like to import the names from excel in a csv file, and then output all the combinations back to excel in another csv file.

我可以弄清楚如何导入和导出csv内容,但是我对执行上述排列的最佳算法更感兴趣.

I can figure out how to do the importing and exporting csv stuff, but i am more interested in the best algorithm to do the above permutations.

如果更容易生成排列,那很好,而且我可以轻松消除excel中的重复项.

If it is easier to generate permutations, that is fine as well as I can easily eliminate duplicates in excel.

谢谢!

推荐答案

您可以使用称为 backtracking的算法技术.

或者,根据您拥有的玩家数量,您可以使用蛮力并进行循环.例如,您可以使用以下命令选择2个前进和1个中心的所有组合(这是一个C ++示例,仅用于说明该技术).

Or, depending on how many players you have, you could use brute force and just loop. For example, you could use the following to select all the combinations of 2 forwards and 1 center (this is a C++ sample just shown to illustrate the technique).

    #include <iostream>
    #include <fstream>
    #include <algorithm>
    #include <numeric>
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>
    using namespace std;

    int main() {
      vector< string > centers;
      vector< string > forwards;
      centers.push_back("joey");
      centers.push_back("rick");
      centers.push_back("sam");

      forwards.push_back("steve");
      forwards.push_back("joe");
      forwards.push_back("harry");
      forwards.push_back("william");

      for(int i = 0; i < centers.size(); ++i) {
        for(int j = 0; j < forwards.size(); ++j) {
          for(int k = j+1; k < forwards.size(); ++k) {
            printf("%s %s %s\n",centers[i].c_str(), forwards[j].c_str(), forwards[k].c_str());
          }
        }
      }
      return 0;
    }

输出:

---------- Capture Output ----------
> "c:\windows\system32\cmd.exe" /c c:\temp\temp.exe
joey steve joe
joey steve harry
joey steve william
joey joe harry
joey joe william
joey harry william
rick steve joe
rick steve harry
rick steve william
rick joe harry
rick joe william
rick harry william
sam steve joe
sam steve harry
sam steve william
sam joe harry
sam joe william
sam harry william

> Terminated with exit code 0.

但是,重要的是要记住,如果您有很多玩家,那么您所做的任何事情都是蛮力",其中包括回溯(回溯与我上面使用的循环相同,只是使用递归)在运行时间中将成倍增长.例如,对于一个5人名单,如果您有10个中锋,20个前锋和18个后卫,则奔跑时间基本上是:

However, it's important to remember that if you have a lot of players, anything you do that's "brute force", which would include backtracking (backtracking is the same idea as the loops I used above, only it uses recursion) is going to grow exponentially in running time. So for example for a 5 man roster, if you have 10 centers, 20 forwards, and 18 guards, then the running time is basically:

10 * 20 * 20 * 18 * 18 = 1,296,000

10 * 20 * 20 * 18 * 18 = 1,296,000

(20 * 20因为我们需要2个防御者,而18 * 18因为我们需要2个守卫).

(20 * 20 because we need 2 fowards, and 18 * 18 because we need 2 guards).

1,296,000的运行时间还不错,但是当您开始谈论9个人名单时,您的运行时间要长得多,因为现在您要处理更多的组合.

1,296,000 is not too bad for running time, but when you start talking about 9 man rosters, you get much higher running times, because now you're dealing with alot more combinations.

因此,这是否可行取决于您拥有多少数据.

So it depends on how much data you have as to whether this is even feasible.

这篇关于C团队中的球员组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆