华为OD机试 - 学生排名(Python/JS/C/C++ 2024 E卷 100分)

在这里插入图片描述

华为OD机试 2024E卷题库疯狂收录中,刷题点这里

专栏导读

本专栏收录于《华为OD机试真题(Python/JS/C/C++)》

刷的越多,抽中的概率越大,私信哪吒,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、3个测试用例、为什么这道题采用XX算法、XX算法的适用场景,发现新题目,随时更新,全天CSDN在线答疑。

一、题目描述

小明来到某学校当老师,需要将学生按考试总分或单科分数进行排名,你能帮帮他吗?

二、输入描述

第1行输入两个整数,学生人数n和科目数量m。0<n<100,0<m<10

第2行输入m个科目名称,彼此之间用空格隔开。科目名称只由英文字母构成,单个长度不超过10个字符。科目的出现顺序和后续输入的学生成绩一一对应。不会出现重复的科目名称。

第3行开始的n行,每行包含一个学生的姓名和该生m个科目的成绩(空格隔开),学生不会重名。学生姓名只由英文字母构成,长度不超过10个字符。成绩是0~100的整数,依次对应第2行中输入的科目。

第n+2行,输入用作排名的科目名称。若科目不存在,则按总分进行排序。

三、输出描述

输出一行,按成绩排序后的学生名字,空格隔开。成绩相同的按照学生姓名字典顺序排序。

1、输入

3 2
yuwen shuxue
fangfang 95 90
xiaohua 88 95
minmin 100 82
shuxue

2、输出

xiaohua fangfang minmin

3、说明

按shuxue成绩排名,依次是xiaohua、fangfang、minmin

四、解题思路

给定学生人数和科目数量,以及每个学生的姓名、各科成绩。要求按照指定科目或总分对学生进行排名,并输出排名结果。

解题思路:

  1. 解析输入,读取学生人数、科目数量、科目名称和学生成绩。
  2. 根据输入的科目名称或总分对学生进行排序。
  3. 输出排序后的学生名单,如果成绩相同,则按照学生姓名的字典顺序排序。

六、Python算法源码

class Student:
    def __init__(self, name, scores):
        self.name = name
        self.scores = scores

    def get_name(self):
        return self.name

    def get_total_score(self):
        return sum(self.scores.values())

def main():
    n, m = map(int, input().split())  # 学生人数和科目数量
    subjects = input().split()  # 科目名称数组

    # 科目名称到索引的映射
    subject_index_map = {subjects[i]: i for i in range(m)}

    students = []

    # 读取学生信息
    for _ in range(n):
        student_info = input().split()
        name = student_info[0]
        scores = {subjects[j]: int(student_info[j + 1]) for j in range(m)}
        students.append(Student(name, scores))

    sort_by_subject = input()  # 排序依据的科目名称

    # 根据排序依据对学生进行排序
    def compare_students(s1, s2):
        if sort_by_subject in subject_index_map:
            score1 = s1.scores.get(sort_by_subject, 0)
            score2 = s2.scores.get(sort_by_subject, 0)
        else:
            score1 = s1.get_total_score()
            score2 = s2.get_total_score()

        if score1 != score2:
            return score2 - score1  # 按成绩降序排序
        else:
            return (s1.get_name()).__lt__(s2.get_name())  # 成绩相同按姓名字典顺序排序

    students.sort(key=lambda s: (s.scores.get(sort_by_subject, s.get_total_score()), s.get_name()), reverse=True)

    # 输出排序后的学生名单
    for student in students:
        print(student.get_name(), end=" ")

if __name__ == "__main__":
    main()

七、JavaScript算法源码

class Student {
    constructor(name, scores) {
        this.name = name;
        this.scores = scores;
    }

    getName() {
        return this.name;
    }

    getTotalScore() {
        return Object.values(this.scores).reduce((total, score) => total + score, 0);
    }
}

function main() {
    const input = require('readline-sync'); // 使用 readline-sync 模块来同步输入
    const n = parseInt(input.question('Enter the number of students: ')); // 学生人数
    const m = parseInt(input.question('Enter the number of subjects: ')); // 科目数量
    
    const subjects = input.question('Enter the subjects: ').split(' '); // 科目名称数组

    // 科目名称到索引的映射
    const subjectIndexMap = {};
    for (let i = 0; i < m; i++) {
        subjectIndexMap[subjects[i]] = i;
    }

    const students = [];

    // 读取学生信息
    for (let i = 0; i < n; i++) {
        const studentInfo = input.question('Enter student info: ').split(' ');
        const name = studentInfo[0];
        const scores = {};
        for (let j = 0; j < m; j++) {
            scores[subjects[j]] = parseInt(studentInfo[j + 1]);
        }
        students.push(new Student(name, scores));
    }

    const sortBySubject = input.question('Enter the subject to sort by: '); // 排序依据的科目名称

    // 根据排序依据对学生进行排序
    students.sort((s1, s2) => {
        let score1, score2;

        if (subjectIndexMap.hasOwnProperty(sortBySubject)) {
            score1 = s1.scores[sortBySubject] || 0;
            score2 = s2.scores[sortBySubject] || 0;
        } else {
            score1 = s1.getTotalScore();
            score2 = s2.getTotalScore();
        }

        if (score1 !== score2) {
            return score2 - score1; // 按成绩降序排序
        } else {
            return s1.getName().localeCompare(s2.getName()); // 成绩相同按姓名字典顺序排序
        }
    });

    // 输出排序后的学生名单
    students.forEach(student => {
        process.stdout.write(student.getName() + ' '); // 输出学生姓名并保持在同一行
    });
}

main();

八、C算法源码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STUDENTS 100
#define MAX_SUBJECTS 10
#define MAX_NAME_LEN 50

typedef struct {
    char name[MAX_NAME_LEN];
    int scores[MAX_SUBJECTS];
} Student;

// 计算学生总成绩
int get_total_score(Student *student, int num_subjects) {
    int total = 0;
    for (int i = 0; i < num_subjects; i++) {
        total += student->scores[i];
    }
    return total;
}

// 比较学生成绩用于排序
int compare_students(const void *a, const void *b, int subject_index, int num_subjects) {
    Student *student1 = (Student *)a;
    Student *student2 = (Student *)b;

    int score1 = (subject_index >= 0) ? student1->scores[subject_index] : get_total_score(student1, num_subjects);
    int score2 = (subject_index >= 0) ? student2->scores[subject_index] : get_total_score(student2, num_subjects);

    // 按成绩降序排序
    if (score1 != score2) {
        return score2 - score1;
    } else {
        // 如果成绩相同,按姓名的字典顺序排序
        return strcmp(student1->name, student2->name);
    }
}

// 自定义的排序函数,用于 `qsort`
int sort_wrapper(const void *a, const void *b) {
    extern int subject_index;
    extern int num_subjects;
    return compare_students(a, b, subject_index, num_subjects);
}

int main() {
    int n, m;
    char subjects[MAX_SUBJECTS][MAX_NAME_LEN]; // 存储科目名称
    Student students[MAX_STUDENTS];            // 存储学生信息
    char sort_by_subject[MAX_NAME_LEN];        // 用于排序的科目名称
    int subject_index = -1;                    // 排序依据的科目索引
    int num_subjects;

    // 读取学生人数和科目数量
    printf("Enter the number of students and subjects: ");
    scanf("%d %d", &n, &m);
    num_subjects = m;

    // 读取科目名称
    printf("Enter the subject names: ");
    for (int i = 0; i < m; i++) {
        scanf("%s", subjects[i]);
    }

    // 读取学生信息
    for (int i = 0; i < n; i++) {
        printf("Enter the student name and scores: ");
        scanf("%s", students[i].name); // 读取学生姓名
        for (int j = 0; j < m; j++) {
            scanf("%d", &students[i].scores[j]); // 读取学生的每科成绩
        }
    }

    // 读取用于排序的科目名称
    printf("Enter the subject to sort by: ");
    scanf("%s", sort_by_subject);

    // 找到用于排序的科目名称的索引
    for (int i = 0; i < m; i++) {
        if (strcmp(subjects[i], sort_by_subject) == 0) {
            subject_index = i;
            break;
        }
    }

    // 使用 qsort 进行排序
    qsort(students, n, sizeof(Student), sort_wrapper);

    // 输出排序后的学生名单
    printf("Sorted students:\n");
    for (int i = 0; i < n; i++) {
        printf("%s ", students[i].name);
    }
    printf("\n");

    return 0;
}

九、C++算法源码

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
using namespace std;

class Student {
public:
    string name;
    map<string, int> scores;

    Student(string name, map<string, int> scores) {
        this->name = name;
        this->scores = scores;
    }

    string getName() const {
        return name;
    }

    int getTotalScore() const {
        int total = 0;
        for (auto const& score : scores) {
            total += score.second;
        }
        return total;
    }
};

int main() {
    int n, m;
    cout << "Enter the number of students and subjects: ";
    cin >> n >> m;

    vector<string> subjects(m);  // 科目名称数组
    map<string, int> subjectIndexMap; // 科目名称到索引的映射

    cout << "Enter the subject names: ";
    for (int i = 0; i < m; i++) {
        cin >> subjects[i];
        subjectIndexMap[subjects[i]] = i;
    }

    vector<Student> students;

    // 读取学生信息
    for (int i = 0; i < n; i++) {
        string name;
        map<string, int> scores;

        cout << "Enter the student name and scores: ";
        cin >> name;
        for (int j = 0; j < m; j++) {
            int score;
            cin >> score;
            scores[subjects[j]] = score;
        }

        students.emplace_back(name, scores);
    }

    string sortBySubject;
    cout << "Enter the subject to sort by: ";
    cin >> sortBySubject;

    // 根据排序依据对学生进行排序
    sort(students.begin(), students.end(), [&](const Student& s1, const Student& s2) {
        int score1, score2;

        if (subjectIndexMap.find(sortBySubject) != subjectIndexMap.end()) {
            score1 = s1.scores.at(sortBySubject); // 指定科目成绩
            score2 = s2.scores.at(sortBySubject);
        } else {
            score1 = s1.getTotalScore(); // 总成绩
            score2 = s2.getTotalScore();
        }

        if (score1 != score2) {
            return score2 < score1; // 按成绩降序排序
        } else {
            return s1.getName() < s2.getName(); // 按姓名字典顺序排序
        }
    });

    // 输出排序后的学生名单
    cout << "Sorted students:\n";
    for (const auto& student : students) {
        cout << student.getName() << " ";
    }
    cout << endl;

    return 0;
}

🏆下一篇:华为OD机试真题 - 简易内存池(Python/JS/C/C++ 2024 E卷 200分)

🏆本文收录于,华为OD机试真题(Python/JS/C/C++)

刷的越多,抽中的概率越大,私信哪吒,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、3个测试用例、为什么这道题采用XX算法、XX算法的适用场景,发现新题目,随时更新,全天CSDN在线答疑。

在这里插入图片描述

华为OD机试-2023真题的考点主要为以下几个类: 1. 数据结构与算法:考察对各种常用数据结构(如数组、链表、栈、队列、树、图等)和算法(如排序、查找、递归、动态规划等)的理解和应用能力,以及对时间复杂度和空间复杂度的析和优化。 2. 编程语言和语法:考察对编程语言(如C++Python等)的基本语法和特性的掌握程度,包括变量、运算符、控制流程、函数、类等。同时还要求考生熟练运用相关的标准库和常用的数据结构和算法。 3. 网络与操作系统:考察对计算机网络和操作系统的基本原理和常用技术的了解,包括网络通信、TCP/IP协议、进程管理、内存管理、文件系统等。要求考生理解并能解决相关的问题。 4. 数据库与SQL:考察对数据库的基本概念和原理的理解,包括数据模型、关系代数、SQL语言等内容。要求考生能够编写和优化SQL查询语句,处理常见的数据库操作和性能问题。 5. 系统设计与架构:考察对大型系统的设计和架构的理解,包括系统需求析、模块划、接口设计等。要求考生能够根据需求或场景提出可行的系统设计方案,并能解决相关的实际问题。 总体来说,华为OD机试-2023真题的考点比较全面,涵盖了计算机科学与技术的多个方面。考生需要具备扎实的数据结构与算法基础,熟悉常用编程语言和标准库的使用,了解网络和操作系统的基本原理,掌握数据库和SQL语言的基本知识,同时具备系统设计和架构的能力。只有在这些方面的基础上才能够应对各种考题,并取得优异的表现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哪 吒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值