前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PAT(乙级)1004.成绩排名(20)

PAT(乙级)1004.成绩排名(20)

作者头像
lexingsen
发布2022-02-25 08:09:14
2080
发布2022-02-25 08:09:14
举报
文章被收录于专栏:乐行僧的博客

PAT 1004.成绩排名(20)

读入 n(>0)名学生的姓名、学号、成绩,分别输出成绩最高和成绩最低学生的姓名和学号。 输入格式: 每个测试输入包含 1 个测试用例,格式为:

代码语言:javascript
复制
第 1 行:正整数 n
第 2 行:第 1 个学生的姓名 学号 成绩
第 3 行:第 2 个学生的姓名 学号 成绩
  ... ... ...
第 n+1 行:第 n 个学生的姓名 学号 成绩

其中姓名学号均为不超过 10 个字符的字符串,成绩为 0 到 100 之间的一个整数,这里保证在一组测试用例中没有两个学生的成绩是相同的。

输出格式: 对每个测试用例输出 2 行,第 1 行是成绩最高学生的姓名和学号,第 2 行是成绩最低学生的姓名和学号,字符串间有 1 空格。

输入样例:

代码语言:javascript
复制
3
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95

输出样例:

代码语言:javascript
复制
Mike CS991301
Joe Math990112

题目分析:排序的使用。 AC代码:

代码语言:javascript
复制
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cassert>
using namespace std;

typedef struct student{
  char name[11];
  char num[11];
  int score;
}Student;

bool cmp(student a,student b){
  return a.score < b.score;
}

int main(){
  int n;
  cin>>n;
  int tmp = n;
  Student student[tmp];
  while(tmp--){
    cin>>student[tmp].name>>student[tmp].num>>student[tmp].score;
    assert(student[tmp].score>=0 && student[tmp].score<=100);
  }
  sort(student,student+n,cmp);
  printf("%s %s\n",student[n-1].name,student[n-1].num);
  printf("%s %s\n",student[0].name,student[0].num);
  return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档