1083. List Grades (25)[结构体排序]

来源:互联网 发布:邓肯生涯最高数据 编辑:程序博客网 时间:2024/05/16 12:36

1. 原题:https://www.patest.cn/contests/pat-a-practise/1083

2. 思路:

题意:结构体排序。
思路:
有多种方法。我用的vector排序,因为每个成绩是唯一的。
排好序,遍历输出。
已AC.

3. 源码:

#include <iostream>#include <algorithm>#include <string>#include <vector>using namespace std;struct Student//定义结构体{bool operator< (const Student &b)//运算符重载,降序{if (m_grade > b.m_grade)return true;elsereturn false;}int m_grade;string m_name;string m_id;};/*题意:结构体排序。思路:有多种方法。我用的vector排序,因为每个成绩是唯一的。排好序,遍历输出。已AC.*/int main(void){//freopen("in.txt", "r", stdin);int N;//总数cin >> N;vector<Student> stu(N);for (int i = 0; i < N; i++)//读入数据{cin >> stu[i].m_name >> stu[i].m_id >> stu[i].m_grade;}sort(&stu[0], &stu[0]+N);int leftInterval, rightInterval;cin >> leftInterval >> rightInterval;int flag = 0;for (int i = 0; i < N; i++)//遍历输出{if (stu[i].m_grade > rightInterval)continue;else if (stu[i].m_grade >= leftInterval){cout << stu[i].m_name << ' ' << stu[i].m_id << endl;flag = 1;}elsebreak;}if (flag == 0)cout << "NONE" << endl;return 0;}


0 0
原创粉丝点击