1083. List Grades (25)(基础,排序)

PAT-A1083,题目地址:https://www.patest.cn/contests/pat-a-practise/1083
本题目比较简单,没有指明数据大小,可以先剔除不在区间内的数据,再排序;或先排序后筛选也可,试试证明时间限制不是问题。
注意:人名和ID的长度为10,则字符串大小连同空字符至少为11。

#include 
#include 

struct Student{
    char name[11];
    char id[11];
    int score;
};

bool cmp(const Student &a, const Student &b){
    return a.score > b.score;
}

int main(){
    int count, low, high;
    Student* students = NULL;
    scanf("%d", &count);
    students = new Student[count];
    for(int i = 0; i < count; i++){
        scanf("%s %s %d", students[i].name, students[i].id, &students[i].score);
    }
    scanf("%d %d", &low, &high);
    if(high < low){ //好像多此一举了,一开始以为这个interval有可能先给出high,不过好像题意说明了是low在前,总之这段话的意思就是swap(low,high)
        high += low;
        low = high - low;
        high -= low;
    }
    std::sort(students, students + count, cmp);
    bool none = true;
    for(int i = 0; i < count; i++){
        if(students[i].score <= high && students[i].score >= low){
            printf("%s %s\n", students[i].name, students[i].id);
            none = false;
        }
    }
    if(none){
        printf("NONE\n");
    }
    return 0;
}

你可能感兴趣的:(1083. List Grades (25)(基础,排序))