1083. List Grades (25)

本文介绍了解决PAT竞赛中排序与筛选问题的方法。首先使用排序算法对数组进行排序,然后遍历所有可能的答案,找出最合适的答案。通过设置边界条件low和high,筛选出符合条件的元素并输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1083

and the source code is as followed.

/*
firstly: sort the array using the algorithm "sort"
secondly: traverse all the possible answer and find the most suitable one.
*/
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>

using namespace std;

struct record
{
    string name;
    string id;
    int grade;
};

bool cmp(record x,record y)
{
    return x.grade > y.grade;
}

int main()
{
    int n;
    record temp;
    vector<record> v;
    cin>>n;
    while (n--)
    {
        cin>>temp.name>>temp.id>>temp.grade;
        v.push_back(temp);
    }
    int low,high;
    cin>>low>>high;

    sort(v.begin(),v.end(),cmp);
    int count = 0;
    for (int i = 0; i < v.size(); i++)
    {
        
        if (v[i].grade>=low && v[i].grade <= high)
        {
            cout<<v[i].name<<" "<<v[i].id<<endl;
            count += 1;
        }
    }
    if (count == 0)
    {
        cout<<"NONE"<<endl;
    }
    
}

this one is easy.and it doesn’t need to talk too much. If there are something to be noticed ,

maybe i think we should notice the boundery.

转载于:https://www.cnblogs.com/maverick-fu/p/3969358.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值