2011-02-07 34 views
1

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

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

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

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

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

谢谢!

回答

1

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

或者,根据你有多少玩家,你可以使用强力和循环。例如,您可以使用以下选项来选择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个人名册时,你会得到更高的运行时间,因为现在你正在处理更多的组合。

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