gpt4 book ai didi

c - 根据规则排列对象所需的数据结构

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:39:49 26 4
gpt4 key购买 nike

类(class)中的 N 名学生互相玩游戏,每个学生与类(class)中的所有其他学生比赛。

另外,类长还要把学生排成一排,每一个学生都输了,就和他前面的那个学生比赛。

For example:

Let there be 4 students A, B, C, and D

Total number of matches: 6( (A,B), (A,C), (A,D), (B,C), (B,D), (C,D) )

Match results:

A def B

C def A

A def D

C def B

B def D

C def D

So the order is: C A B D

为了存储学生对象,我使用了以下数据结构:

struct node {
int student_id;
int wins;
}

我正在使用优先队列来安排学生,优先考虑获胜较多的学生。

如果两个或多个对象具有相同的优先级,那么我该如何安排它们?

我正在考虑根据谁胜谁负保留一个有向图,然后进行拓扑排序。会成功吗?

最佳答案

First student will play against N-1 players

Second student shall play against N-2 remaining players

.

.

.

N-1 shall play against 1 remaining player.

Nth player has played against all by now.

因此参加比赛的次数:

N-1+N-2....+1+0= N(N-1)/2


I'm thinking of keeping a Directed graph based on who wins on whom, and then do a topological sort, will it work?

当图形是非循环的时它应该工作。当 A def 时,只需确保方向是从 A 到 B。 B. 否则你将不得不反转堆栈跟踪。

我对周期性情景持怀疑态度。但是有一个更简单的方法:

假设有 5 个玩家:A B C D E

根据获胜次数对列表进行排序,如下所示:

x def y(x->y)

A->B

A->D

B->C

B->D

B->E

C->A

D->C

D->E

E->A

E->C

现在从上面列出的第一场胜利开始。

A->B字符串:A B

A->D现在 D 可以在 B 之前或之后出现。在查找时,B->D。隐含字符串:A B D

B->C查找时,D->C字符串:A B D C

B->E查找时,E->C这意味着 E 必须先于 C。On-look up D->E

字符串:A B D E C

字符串长度为 5。停止。

关于c - 根据规则排列对象所需的数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45062925/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com