开发者

combinations of players for a team in C

开发者 https://www.devze.com 2023-02-09 15:44 出处:网络
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 p

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 a开发者_JAVA百科ll 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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消