PAT (Basic Level) Practice (中文) 1004 成绩排名 (20 分)

简介: PAT (Basic Level) Practice (中文) 1004 成绩排名 (20 分)

题目描述


读入 n(>0)名学生的姓名、学号、成绩,分别输出成绩最高和成绩最低学生的姓名和学号。


输入格式:


每个测试输入包含 1 个测试用例,格式为


第 1 行:正整数 n

第 2 行:第 1 个学生的姓名 学号 成绩

第 3 行:第 2 个学生的姓名 学号 成绩

… … …

第 n+1 行:第 n 个学生的姓名 学号 成绩

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


输出格式:


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


输入样例:


3

Joe Math990112 89

Mike CS991301 100

Mary EE990830 95

结尾无空行


输出样例:


Mike CS991301

Joe Math990112

结尾无空行


代码

#include<bits/stdc++.h>
using namespace std;
struct student {
  char name[15];
  char id[15];
  int score;
} temp,ans_min,ans_max ;
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  scanf("%d",&n);
  ans_min.score=100;
  ans_max.score=-1;
  for(int i=0;i<n;i++)
  {
    scanf("%s %s %d",&temp.name,&temp.id,&temp.score);
    if(temp.score<ans_min.score)
    {
      ans_min=temp;
    }
    if(temp.score>ans_max.score)
    {
      ans_max=temp;
    }
  }
  printf("%s %s\n",ans_max.name,ans_max.id);
  printf("%s %s\n",ans_min.name,ans_min.id);
  return 0;
 } 

分析


找出最大值和最小值,刚开始想到的是使用数组下标最为分数,使用结构体数组存储,但是太占用存储空间,改进使用三个变量进行存储,一个是读入数据temp,还有一个是最大值,还有一个是最小值,每读入一个数据就要进行比较大小,之后将数据刷新,最后将数据打印。


总结


找最大最小时候,可以直接使用覆盖数据来进行。


相关文章
|
10月前
|
C语言 C++
PAT (Basic Level) Practice (中文)1099 性感素数(20分)
“性感素数”是指形如 (p, p+6) 这样的一对素数。之所以叫这个名字,是因为拉丁语管“六”叫“sex”(即英语的“性感”)。(原文摘自 http://mathworld.wolfram.com/SexyPrimes.html) 现给定一个整数,请你判断其是否为一个性感素数。
85 0
|
算法
PAT (Basic Level) Practice (中文)1028. 人口普查(20分)
PAT (Basic Level) Practice (中文)1028. 人口普查(20分)
76 0
|
存储
PAT (Basic Level) Practice (中文) 1041 考试座位号 (15 分)
PAT (Basic Level) Practice (中文) 1041 考试座位号 (15 分)
64 0
PAT (Basic Level) Practice (中文) 1041 考试座位号 (15 分)
PAT (Basic Level) Practice (中文) 1016 部分A+B (15 分)
PAT (Basic Level) Practice (中文) 1016 部分A+B (15 分)
64 0
|
测试技术
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
76 0
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
|
C语言
PAT (Basic Level) Practice (中文) B1026 程序运行时间 (15 分)
PAT (Basic Level) Practice (中文) B1026 程序运行时间 (15 分)
96 0
PAT (Basic Level) Practice (中文) B1046 划拳 (15 分)
PAT (Basic Level) Practice (中文) B1046 划拳 (15 分)
59 0
PAT (Basic Level) Practice (中文) 1036 跟奥巴马一起编程 (15 分) p89
PAT (Basic Level) Practice (中文) 1036 跟奥巴马一起编程 (15 分) p89
125 0
PAT (Basic Level) Practice (中文)- 1052 卖个萌(20 分)
PAT (Basic Level) Practice (中文)- 1052 卖个萌(20 分)
78 0
PAT (Basic Level) Practice (中文)- 1073 多选题常见计分法(20 分)
PAT (Basic Level) Practice (中文)- 1073 多选题常见计分法(20 分)
102 0