PAT-BASIC1004——成绩排名

我的PAT-BASIC代码仓:https://github.com/617076674/PAT-BASIC

原题链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805321640296448

题目描述:

知识点:结构体

思路:用C++的结构体或者Java的类存储数据

时间复杂度是O(n),其中n为输入的记录数。对于空间复杂度,如果存储了所有的记录值,那就是O(n)。如果只存储最大分数和最小分数的记录值,那就是O(1)。

C++代码:

#include<iostream>
#include<string>

using namespace std;

struct Student {
	string name;
	string number;
	int score;
};

int main() {
	int count;
	cin >> count;

	string name;
	string number;
	int score;
	cin >> name >> number >> score;

	Student lowestStudent;
	Student highestStudent;

	lowestStudent.name = name;
	lowestStudent.number = number;
	lowestStudent.score = score;

	highestStudent.name = name;
	highestStudent.number = number;
	highestStudent.score = score;

	for (int i = 1; i < count; i++) {
		cin >> name >> number >> score;
		if (score > highestStudent.score) {
			highestStudent.name = name;
			highestStudent.number = number;
			highestStudent.score = score;
		}
		if (score < lowestStudent.score) {
			lowestStudent.name = name;
			lowestStudent.number = number;
			lowestStudent.score = score;
		}
	}

	cout << highestStudent.name << " " << highestStudent.number << endl;
	cout << lowestStudent.name << " " << lowestStudent.number << endl;
}

C++解题报告:

JAVA代码:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int count = scanner.nextInt();
        scanner.nextLine();
        ArrayList<Student> arrayList = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            String[] messages = scanner.nextLine().split(" ");
            arrayList.add(new Student(messages[0], messages[1], Integer.parseInt(messages[2])));
        }
        Student maxScoreStudent = arrayList.get(0);
        Student minScoreStudent = arrayList.get(0);
        for (int i = 1; i < count; i++) {
            if(maxScoreStudent.score < arrayList.get(i).score){
                maxScoreStudent = arrayList.get(i);
            }
            if(minScoreStudent.score > arrayList.get(i).score){
                minScoreStudent = arrayList.get(i);
            }
        }
        System.out.println(maxScoreStudent.name + " " + maxScoreStudent.number);
        System.out.println(minScoreStudent.name + " " + minScoreStudent.number);
    }
}

class Student{
    public String name;
    public String number;
    public Integer score;

    public Student(String name, String number, Integer score){
        this.name = name;
        this.number = number;
        this.score = score;
    }
}

JAVA解题报告:

 

### PAT 乙级 1004 成绩排名题目解析 #### 题目描述 该题目要求读取 `n` 名学生的信息,每条记录包含学生的姓名、学号以及成绩。目标是找出并输出成绩最高和最低的学生的姓名及其对应的学号。 输入数据的第一行为一个正整数 `n`,表示参与评估的学生总数;随后的每一行则依次给出每位同学的具体资料——名字、编号与分数[^2]。需要注意的是,这里的名称长度不会超过十个字母,并且所有人的得分都在零至一百分之间不重复存在[^3]。 #### 解决方案概述 为了实现上述功能,可以采用如下策略: - 创建结构体来存储单个考生的数据; - 使用数组保存全部学员信息以便后续处理; - 设定变量追踪最大最小值连同对应位置索引; - 最终遍历整个列表找到最优解后打印结果即可。 下面是具体的 C++ 实现方法: ```cpp #include <iostream> #include <vector> using namespace std; struct Student { string name; string id; int score; }; int main() { vector<Student> students; int n, maxScore = -1, minScore = 101, maxIndex = 0, minIndex = 0; cin >> n; for(int i=0;i<n;++i){ Student temp; cin>>temp.name>>temp.id>>temp.score; if(temp.score > maxScore){ maxScore=temp.score; maxIndex=i; } if(temp.score<minScore){ minScore=temp.score; minIndex=i; } students.push_back(temp); } cout << "Highest Score:\nName: "<<students[maxIndex].name<<"\tID:"<<students[maxIndex].id<<endl; cout << "\nLowest Score:\nName: "<<students[minIndex].name<<"\tID:"<<students[minIndex].id<<endl; } ``` 此程序首先定义了一个名为 `Student` 的结构用来封装个人详情。接着通过循环接收用户输入并将之存入向量容器之中同时更新极值标记直至结束为止。最后按照指定格式显示最终答案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值