您可以使用称为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个人名册时,你会得到更高的运行时间,因为现在你正在处理更多的组合。
所以这取决于你有多少数据是否可行。
来源
2011-02-07 15:43:46
dcp