博客
关于我
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/

    你可能感兴趣的文章
    mt_rand
    查看>>
    mysql -存储过程
    查看>>
    mysql /*! 50100 ... */ 条件编译
    查看>>
    mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
    查看>>
    mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
    查看>>
    mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
    查看>>
    mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
    查看>>
    MySQL 8.0 恢复孤立文件每表ibd文件
    查看>>
    MySQL 8.0开始Group by不再排序
    查看>>
    mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
    查看>>
    multi swiper bug solution
    查看>>
    MySQL Binlog 日志监听与 Spring 集成实战
    查看>>
    MySQL binlog三种模式
    查看>>
    multi-angle cosine and sines
    查看>>
    Mysql Can't connect to MySQL server
    查看>>
    mysql case when 乱码_Mysql CASE WHEN 用法
    查看>>
    Multicast1
    查看>>
    mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
    查看>>
    MySQL Cluster 7.0.36 发布
    查看>>
    Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
    查看>>