首页 > 编程语言 > 详细

PAT 1004(成绩排名)(C++)

时间:2020-01-20 18:02:53      阅读:56      评论:0      收藏:0      [点我收藏+]

本题主要掌握对字符串的基本知识,例如输入格式,输出格式还有一些函数

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

C++代码

#include<iostream>
#include<string>
using namespace std;
//学生的结构体,也可以用类更方便
struct student
{
    string name ;
    string  number ;
    int score;
}stu[100000];
int main()
{
    int n = 0;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> stu[i].name >> stu[i].number >> stu[i].score;
        //判断是否符合输入格式,不符合一直循环
        while (stu[i].name.length() >10 || stu[i].number.length() > 10 || stu[i].score < 0 || stu[i].score>100)
        {
            cin >> stu[i].name >> stu[i].number >> stu[i].score;
        }
    }
    int index_max = 0, index_min = 0;
    int max_score = 0, min_score = 0;
    int k = 0;
    //提取最大最小分数所在的下标
    while (n--)
    {
        if (max_score <= stu[k].score)
        {
            max_score = stu[k].score;
            index_max = k;
        }
        if (min_score>=stu[k].score)
        {
            min_score = stu[k].score;
            index_min = k;
        }
        k++;
    }
    cout << stu[index_max].name << " " << stu[index_max].number << endl;
    cout << stu[index_min].name << " " << stu[index_min].number << endl;
    return 0;
}

PAT 1004(成绩排名)(C++)

原文:https://www.cnblogs.com/zongji/p/12218516.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!