博客
关于我
1004 成绩排名 (20分)
阅读量:540 次
发布时间:2019-03-09

本文共 1471 字,大约阅读时间需要 4 分钟。

要解决这个问题,我们需要读取n名学生的姓名、学号和成绩,然后分别输出成绩最高和最低的学生的姓名和学号。以下是详细的解决方案:

方法思路

  • 读取输入:首先读取输入的学生数n,然后循环读取每一行,每行包括学生的姓名、学号和成绩。
  • 存储数据:将姓名、学号和成绩分别存储在向量中。
  • 寻找最大值和最小值:遍历成绩,记录最高和最低的成绩对应的学生信息。
  • 输出结果:分别输出成绩最高和最低学生的姓名和学号。
  • 解决代码

    #include 
    #include
    #include
    using namespace std;int main() { int n; cin >> n; vector
    names(n); vector
    nums(n); vector
    scores(n); for (int i = 0; i < n; ++i) { string line; getline(cin, line); size_t first_space = line.find(' '); size_t second_space = line.find(' ', first_space + 1); string name = line.substr(0, first_space); string num = line.substr(first_space + 1, second_space - first_space - 1); string score_str = line.substr(second_space + 1); int score = stoi(score_str); names[i] = name; nums[i] = num; scores[i] = score; } int max_score = scores[0]; int min_score = scores[0]; int max_index = 0; int min_index = 0; for (int i = 1; i < n; ++i) { if (scores[i] > max_score) { max_score = scores[i]; max_index = i; } if (scores[i] < min_score) { min_score = scores[i]; min_index = i; } } cout << names[max_index] << ' ' << nums[max_index] << endl; cout << names[min_index] << ' ' << nums[min_index] << endl; return 0;}

    代码解释

  • 读取输入:首先读取n的值,然后循环读取每一行信息。使用getline函数来读取每一行数据。
  • 分割姓名、学号和成绩:使用find方法找到字符串中的空格,分割出姓名、学号和成绩。
  • 转换成绩:将成绩字符串转换为整数,存储到scores向量中。
  • 寻找最高分和最低分:遍历成绩向量,记录最高分和最低分对应的学生索引。
  • 输出结果:根据记录的索引,分别输出最高分和最低分学生的姓名和学号。
  • 这种方法高效且直接,能够处理所有符合题目要求的输入情况。

    转载地址:http://fceiz.baihongyu.com/

    你可能感兴趣的文章
    multi-angle cosine and sines
    查看>>
    Mysql Can't connect to MySQL server
    查看>>
    mysql case when 乱码_Mysql CASE WHEN 用法
    查看>>
    Multicast1
    查看>>
    MySQL Cluster 7.0.36 发布
    查看>>
    Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
    查看>>
    MySQL Cluster与MGR集群实战
    查看>>
    multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
    查看>>
    mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
    查看>>
    Multiple websites on single instance of IIS
    查看>>
    mysql CONCAT()函数拼接有NULL
    查看>>
    multiprocessing.Manager 嵌套共享对象不适用于队列
    查看>>
    multiprocessing.pool.map 和带有两个参数的函数
    查看>>
    MYSQL CONCAT函数
    查看>>
    multiprocessing.Pool:map_async 和 imap 有什么区别?
    查看>>
    MySQL Connector/Net 句柄泄露
    查看>>
    multiprocessor(中)
    查看>>
    mysql CPU使用率过高的一次处理经历
    查看>>
    Multisim中555定时器使用技巧
    查看>>
    MySQL CRUD 数据表基础操作实战
    查看>>