c - 如何根据 C 中的 2 个字段对结构进行排序?

标签 c sorting quicksort ranking bubble-sort

我在 Hangman 游戏中使用了一个结构,用户根据错误尝试的次数获得积分。

我的问题是如何对它们进行排名,同时考虑积分 (pontos) 和游戏次数 (n_jogos)。

例如:

  • 拥有 10 分且只玩过一次的用户应该排在拥有 15 分且玩了 2 场比赛的用户之前。

到目前为止,我已经尝试:

  • 实现 qsort(),但由于我不太了解它,所以无法让它工作。

  • 实现冒泡排序,该排序适用于按游戏数量排序,但不适用于同时按游戏数量和分数排序。

非常感谢任何帮助,谢谢。

最佳答案

A user that has 10 points and only played once should come before a user that has 15 points that played 2 games.

那么,是不是先按场次再按积分排名呢?我会假设是,但如果不是,那么您必须决定订购标准是什么。这是您可以传递给 qsort(3) 的函数。调味。

typedef struct {
    char nome[50], password[50];
    int pontos, n_jogos;
} Utilizador;

int cmp ( const void *A, const void *B ) {
    const Utilizador *a=A, *b=B;

    if( a->jogos == b->jogos) {
        if( a->pontos == b-pontos ) return 0;
        return a->pontos < b-pontos? -1 : 1;
    }
    return  a->jogos < b->jogos? -1 : 1;
}

关于c - 如何根据 C 中的 2 个字段对结构进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53876564/

相关文章:

c - 警告 : passing argument 1 of ‘calculation’ makes pointer from integer without a cast [enabled by default]

java - 比较方法: Object arg?

r - 查找一个向量中小于另一向量中元素的数量

algorithm - 快速排序算法

c# - 快速排序算法 - 三的中位数

c - 如何在C套接字编程中的聊天客户端程序中使用select()?

projecteuler中问题10的混淆解决方案

c++ - C/C++ 套接字代理

excel - 用于排序和过滤的数据结构

快速排序的 C 实现在 arr[0] 处产生垃圾值