C组球员的组合

我正在试图组成一个篮球队的球员的所有组合。 假设有5个职位(SG,PG,SF,PF,C),我需要用一个9名选手填补一只公鸡,除了只有1个的位置外,每个位置都有2个。

假设每个职位有10名选手,我怎样才能生成所有可能排列的列表。

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

我可以弄清楚如何导入和导出csv的东西,但我更感兴趣的最好的algorithm做上述排列。

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

谢谢!

您可以使用称为回溯的algorithm技术。

或者,根据你有多less玩家,你可以使用暴力和循环。 例如,您可以使用以下选项来select2个向前和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" /cc:\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. 

然而,重要的是要记住,如果你有很多玩家,那么你所做的任何“蛮力”的事情,包括回溯(backtracking和上面使用的回路是一样的,只有它使用recursion)将会增长运行时间呈指数级增长。 比如说一个5人名单,如果你有10个中锋,20个前锋,18个后卫,那么运行时间基本上是:

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

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

运行时间为129.6万,但是当你开始谈论9个人的名单时,你的运行时间会更长,因为现在你正在处理更多的组合。

所以这取决于你有多less数据是否可行。