C 球队的球员组合
我正在尝试生成所有球员组合来组成一支篮球运动员团队。 假设有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用一种称为回溯的算法技术。
或者,根据您有多少玩家,您可以使用暴力并循环。例如,您可以使用以下代码来选择 2 个前锋和 1 个中锋的所有组合(这是一个 C++ 示例,只是为了说明该技术而显示)。
输出:
然而,重要的是要记住,如果你有很多玩家,那么你所做的任何事情都是“蛮力”,其中包括回溯(回溯与我上面使用的循环的想法相同,只是它使用递归)运行时间呈指数增长。例如,对于一个 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).
Output:
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.