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

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

#include <cstdio>
#include <algorithm>

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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容