C 球队的球员组合

发布于 2024-10-16 09:22:30 字数 315 浏览 5 评论 0原文

我正在尝试生成所有球员组合来组成一支篮球运动员团队。 假设有 5 个位置(SG、PG、SF、PF、C),我需要在一只公鸡中容纳 9 名球员,每个位置 2 名球员,除了中锋位置,只有 1 名

球员。假设每个位置有 10 名球员,如何生成所有可能排列的列表。

我想将 excel 中的名称导入到 csv 文件中,然后将所有组合输出到另一个 csv 文件中的 excel 中。

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

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

谢谢!

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.

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

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.

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.

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

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九歌凝 2024-10-23 09:22:30

您可以使用一种称为回溯的算法技术。

或者,根据您有多少玩家,您可以使用暴力并循环。例如,您可以使用以下代码来选择 2 个前锋和 1 个中锋的所有组合(这是一个 C++ 示例,只是为了说明该技术而显示)。

    #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 名后卫,那么运行时间基本上是:

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

(20 * 20 因为我们需要 2 个前锋, 18 * 18 因为我们需要 2 个守卫)。

1,296,000 对于运行时间来说并不算太糟糕,但是当你开始谈论 9 人名单时,你会得到更高的运行时间,因为现在你要处理更多的组合。

因此,这取决于您有多少数据来判断这是否可行。

You could use an algorithmic technique called backtracking.

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

Output:

---------- 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.

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文