PAT 1025

时间:2021-07-16
本文章向大家介绍PAT 1025,主要包括PAT 1025使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

卡在sort的cmp上了

忘记对id排序了

#include<iostream>
#include<vector>
#include <unordered_set>
#include <unordered_map>
#include <cstring>
#include <stdio.h>
#include <algorithm>

using namespace std;
struct Student{
    char id[15];
    int group;
    int score;
    int group_num;
    int all_num;

};
Student stu[50000];
bool cmp(Student s1,Student s2){
    if(s1.score!=s2.score)
    return s1.score>s2.score;
    else{
        return strcmp(s1.id,s2.id)<0;
    }
}
int main() {
    int group_num;
    scanf("%d", &group_num);
    int now_group = 1;
    int all_student_num = 0;
    int i = 0;//student[i]
    while (now_group <= group_num) {//别忘更新new_group
        int student_num;
        scanf("%d", &student_num);
        all_student_num += student_num;
        int student_num1 = student_num;
        int q=i;
        while (student_num--) {
            scanf("%s %d", stu[i].id, &stu[i].score);
            stu[i].group = now_group;
            i++;
        }
        sort(stu + q, stu + q + student_num1, cmp);
        int mingci = 1;

        stu[q].group_num = mingci;
        mingci++;
        for (int j = 1; j < student_num1; ++j) {

            if (stu[q + j].score == stu[q + j - 1].score) {

                stu[q + j].group_num = stu[q + j - 1].group_num;
                mingci++;
            } else {
                stu[q + j].group_num = mingci;
                mingci++;
            }

        }
        now_group++;

    }
    sort(stu, stu + all_student_num, cmp);
    int mingci = 1;
    stu[0].all_num = mingci;
    mingci++;
    for (int j = 1; j < all_student_num; ++j) {
        if (stu[j].score == stu[j - 1].score) {
            stu[j].all_num = stu[j - 1].all_num;
            mingci++;
        } else {
            stu[j].all_num = mingci;
            mingci++;
        }

    }
    printf("%d\n", all_student_num);
    for (int j = 0; j < all_student_num; ++j) {
        printf("%s %d %d %d\n", stu[j].id, stu[j].all_num, stu[j].group, stu[j].group_num);

    }
   


}
为了自己,和那些爱你的人

原文地址:https://www.cnblogs.com/zhmlzhml/p/15021380.html