华为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
四、解题思路
给定学生人数和科目数量,以及每个学生的姓名、各科成绩。要求按照指定科目或总分对学生进行排名,并输出排名结果。
解题思路:
- 解析输入,读取学生人数、科目数量、科目名称和学生成绩。
- 根据输入的科目名称或总分对学生进行排序。
- 输出排序后的学生名单,如果成绩相同,则按照学生姓名的字典顺序排序。
六、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在线答疑。