【深基7.习9】培训

GitHub跳转原题关系图返回列表

用元组表示学员记录,函数返回年龄加一且成绩提升 20% 后不超过 600 的新记录。

OJ: luogu

题目 ID: P5744

难度:入门

标签:模拟结构体函数python

日期: 2026-07-15 21:22

题意

给出若干学员的姓名、年龄、成绩。培训一年后,年龄加一,成绩提升 20%,但最高不超过 600。输出培训后的信息。

思路

Python 中可以用元组保存一条学员记录:

python
(name, age, score)

写函数 train(student),返回新的记录:

  • 年龄 age + 1
  • 成绩 min(600, int(score * 1.2))

本题成绩保证是 5 的倍数,提升 20% 后仍为整数。

Python 知识

  • /home/rainboy/mycode/hugo-blog/content/program_language/python/collections_toolkit.md:元组适合保存固定字段记录。
  • /home/rainboy/mycode/hugo-blog/content/program_language/python/input_output_and_strings.md:混合字段先 split() 再转换整数。
  • min(600, value) 可以表达“不超过 600”。
  • 函数返回元组,可以一次返回多个字段。

代码

python
def train(student):
    name, age, score = student
    new_age = age + 1
    new_score = min(600, int(score * 1.2))
    return name, new_age, new_score


n = int(input())

for _ in range(n):
    name, age, score = input().split()
    trained = train((name, int(age), int(score)))
    print(trained[0], trained[1], trained[2])
cpp
/**
 * Author by Rainboy blog: https://rainboylv.com github: https://github.com/rainboylvx
 * rbook: -> https://rbook.roj.ac.cn  https://rbook2.roj.ac.cn
 * rainboy的学习导航网站: https://idx.roj.ac.cn
 * create_at: 2026-07-27 00:00
 * update_at: 2026-07-27 00:00
 */

#include <bits/stdc++.h>
using namespace std;

struct Student {
    char name[25];
    int age;
    int score;
};

int n;

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        Student s;
        cin >> s.name >> s.age >> s.score;
        // 培训后:年龄 +1,成绩 *1.2 但不超过 600
        s.age++;
        s.score = min(600, int(s.score * 1.2));
        cout << s.name << " " << s.age << " " << s.score << "\n";
    }
    return 0;
}

复杂度

每名学员只处理一次,时间复杂度是 O(n)O(n),空间复杂度是 O(1)O(1)

总结

结构体训练题在 Python 中可以先用元组模拟记录,再用函数接收记录并返回更新后的记录。