惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
有赞技术团队
有赞技术团队
S
SegmentFault 最新的问题
宝玉的分享
宝玉的分享
量子位
小众软件
小众软件
The Cloudflare Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
G
Google Developers Blog
博客园 - 叶小钗
H
Help Net Security
Jina AI
Jina AI
Y
Y Combinator Blog
Last Week in AI
Last Week in AI
GbyAI
GbyAI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research
MyScale Blog
MyScale Blog
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Vercel News
Vercel News

OhYee 博客

小鹏辅助驾驶测评|OhYee 博客 小鹏非支持手机开启自动解锁|OhYee 博客 使用函数计算实现 301 重定向|OhYee 博客 针对 HTML 内容使用 Ant Design 图片弹框|OhYee 博客 博客进程泄露及僵尸进程解决|OhYee 博客 蓝易云服务器体验|OhYee 博客 SSH 调起本地 VSCode|OhYee 博客 【2022 秋招内推】阿里云后端研发工程师|OhYee 博客 使用函数计算获取 IP 地址信息|OhYee 博客 正确获取客户端 IP/HTTP Header 也可能重复|OhYee 博客 评测 Oculus Quest2 及 BigScreen|OhYee 博客 NextJS 热重载保留状态|OhYee 博客 如何优雅地贴 gist 代码|OhYee 博客 Linux 精细化文件权限|OhYee 博客 VSCode 容器开发环境|OhYee 博客 Clash 的不兼容更新排查|OhYee 博客 Zeek 导出 PCAP|OhYee 博客 记一次 ssh 配置问题|OhYee 博客 Git Commit 规范化工具|OhYee 博客 谈谈《星之卡比-探索发现》|OhYee 博客 VSCode 快捷键绑定 Shell 命令|OhYee 博客 ASN.1 语法及 X.509 证书格式解析解析|OhYee 博客 腾讯企业邮箱忽略 MX 记录发信|OhYee 博客 Chrome/Edge 标签组插件|OhYee 博客 【应届内推】阿里云后端研发工程师|OhYee 博客 损坏的 Typecho 备份处理为 JSON|OhYee 博客 VS Code VIM 插件高效使用|OhYee 博客 SSH 正反向代理|OhYee 博客 Let's Encrypt 根证书过期引发的问题|OhYee 博客 OpenWRT 忽略内核依赖|OhYee 博客
PAT乙级 1004.成绩排名|OhYee 博客
2018-06-06 · via OhYee 博客

这是一篇最后编辑于 8 年前 的文章,其内容可能与目前实际情况差异较大,请注意甄别

题目

原题链接

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

输入格式:每个测试输入包含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

解析

排序下,找出来最大最小的即可

代码

C++解法

#include <algorithm>
#include <cstdio>
const int maxn = 105;

struct Node {
    char name[maxn];
    char id[maxn];
    int score;
    void input() { scanf("%s%s%d", name, id, &score); }
    void print() { printf("%s %s\n", name, id); }
    bool operator<(const Node &rhs) const { return score > rhs.score; }
};
Node student[maxn];

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        student[i].input();
    std::sort(student, student + n);
    student[0].print();
    student[n-1].print();
    return 0;
}

Python解法

n = int(input())

minS = []
maxS = []

for i in range(n):
    s = input().split(' ')
    if not minS or int(minS[2]) > int(s[2]):
        minS = s
    if not maxS or int(maxS[2]) < int(s[2]):
        maxS = s
    # print(s,minS,maxS)

print(maxS[0],maxS[1])
print(minS[0],minS[1])

Java解法

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

class Node implements Comparable<Node> {
    private String name;
    private String id;
    private int score;

    public Node(String _name, String _id, int _score) {
        this.name = _name;
        this.id = _id;
        this.score = _score;
    }

    public void print() {
        System.out.printf("%s %s\n", name, id, score);
    }

    @Override
    public int compareTo(Node rhs) {
        return this.score - rhs.score;
    }
}

class Main {
    static Scanner in;

    public static void main(String[] args) {
        in = new Scanner(System.in);
        int n = in.nextInt();
        ArrayList<Node> students = new ArrayList<Node>();
        for (int i = 0; i < n; ++i) {
            String name = in.next();
            String id = in.next();
            int score = in.nextInt();
            students.add(new Node(name, id, score));
        }
        Collections.sort(students);

        
        students.get(n-1).print();
        students.get(0).print();

        in.close();
    }
}