PATA1025PAT Ranking

红太狼 2021-11-02 08:36 328阅读 0赞
  • 需要注意的就是sort函数的应用,还有自己比较函数cmp的编写
  • 在一个就是结构体的设计,排序时的考室内的排序,数组下标的处理

    参考代码:

    define _CRT_SECURE_NO_WARNINGS

    include

    include

    include

    include

    using namespace std;

    struct Student
    {

    1. char id[15];//准考证号
    2. int score;//分数
    3. int location_number;//考室号
    4. int local_rank;//每个考室的排名

    }stu[30010];

    bool cmp(Student a, Student b)//比较函数
    {

    1. if (a.score != b.score) return a.score > b.score;
    2. else return strcmp(a.id, b.id) < 0;

    }

    int main()
    {

    1. int n, k, num = 0;//n是考室数,k是每个考室的人数,num是总人数
    2. scanf("%d", &n);
    3. for (int i = 1; i <= n; i++)
    4. {
    5. scanf("%d", &k);
    6. for (int j = 0; j < k; j++)
    7. {
    8. scanf("%s %d", stu[num].id, &stu[num].score);
    9. stu[num].location_number = i;
    10. num++;
    11. }
    12. sort(stu + num - k, stu + num, cmp);//每个考室段的考生进行排序
    13. stu[num - k].local_rank = 1;//给每个考生的最高分排名标记为1
    14. for (int j = num - k + 1; j < num; j++)
    15. {
    16. if (stu[j].score == stu[j - 1].score)
    17. {
    18. stu[j].local_rank = stu[j - 1].local_rank;
    19. }
    20. else
    21. {
    22. stu[j].local_rank = j + 1 - (num - k);
    23. }
    24. }
    25. }
    26. printf("%d\n", num);
    27. sort(stu, stu + num, cmp);//给怎么所有考生进行排序
    28. int r = 1;
    29. for (int i = 0; i < num; i++)//直接进行输出
    30. {
    31. if (i > 0 && stu[i].score != stu[i - 1].score)
    32. {
    33. r = i + 1;
    34. }
    35. printf("%s ", stu[i].id);
    36. printf("%d %d %d\n", r, stu[i].location_number, stu[i].local_rank);
    37. }
    38. system("pause");
    39. return 0;

    }

转载于:https://www.cnblogs.com/tsruixi/p/11257222.html

发表评论

表情:
评论列表 (有 0 条评论,328人围观)

还没有评论,来说两句吧...

相关阅读

    相关 PAT乙级1025

    1025 反转链表 (25 分) 给定一个常数 K 以及一个单链表 L,请编写程序将 L 中每 K 个结点反转。例如:给定 L 为 1→2→3→4→5→6,K 为 3,则输出

    相关 PATA1025PAT Ranking

    需要注意的就是sort函数的应用,还有自己比较函数cmp的编写 在一个就是结构体的设计,排序时的考室内的排序,数组下标的处理 参考代码: