鱼C论坛

 找回密码
 立即注册
查看: 1250|回复: 1

[已解决]求助一下

[复制链接]
发表于 2023-11-11 16:15:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
7-4 寻找最高分
分数 5
作者 李军
单位 陕西理工大学
  给定n(n⩾1)个学生的学号、姓名和3门课的考试成绩。编写程序找出总分最高的学生,并输出其学号、姓名和总分。如果有多个相同的最高分,则输出所有最高分学生的信息。
要求:
  存储学生信息及考试成绩的变量用如下结构类型来定义。

struct  Student
{
    char num[11];        //学号,最多10个字符
    char name[11];       //姓名, 最多10个字符
    int s1,s2,s3;        //三门课的考试成绩
    int total;           //总成绩   
} ;
typedef struct Student Student;  //声明了一个结构类型Student类型
输入格式:
输入在一行中给出非负整数n(n⩾1)。随后n行,每行给出一个学生的信息,格式为学号 学号 姓名 成绩1 成绩2 成绩3,中间以空格分隔。
要求:
学号、姓名中不包括空白字符(空格、tab符)和空字符串。

输出格式:
在一行中输出总分最高学生的姓名、学号和总分,间隔一个空格。题目保证这样的学生是唯一的。

输入样例:
5
2109001 HuangJie 78 83 79
2109002 Liuhaipeng 79 80 77
2109003 Wangqiang 87 86 76
2109004 Liangfeng 92 89 79
2109005 Chengmeng 80 82 75
输出样例:
在这里给出相应的输出。例如:

2109004 Liangfeng 260


这题用C或者C++怎么做
最佳答案
2023-11-11 16:15:43
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;

  4. struct Student
  5. {
  6.     char num[11];       //学号,最多10个字符
  7.     char name[11];      //姓名, 最多10个字符
  8.     int s1, s2, s3;     //三门课的考试成绩
  9.     int total;          //总成绩
  10. };

  11. int main()
  12. {
  13.     int n;
  14.     cin >> n;
  15.     Student stu[n];
  16.     for (int i = 0; i < n; i++)
  17.     {
  18.         cin >> stu[ i].num >> stu[ i].name >> stu[ i].s1 >> stu[ i].s2 >> stu[ i].s3;
  19.         stu[ i].total = stu[ i].s1 + stu[ i].s2 + stu[ i].s3;
  20.     }
  21.     int maxTotal = -1;
  22.     for (int i = 0; i < n; i++)
  23.     {
  24.         if (stu[ i].total > maxTotal)
  25.         {
  26.             maxTotal = stu[ i].total;
  27.         }
  28.     }
  29.     for (int i = 0; i < n; i++)
  30.     {
  31.         if (stu[ i].total == maxTotal)
  32.         {
  33.             cout << stu[ i].num << " " << stu[ i].name << " " << stu[ i].total << endl;
  34.         }
  35.     }
  36.     return 0;
  37. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-11 16:15:43 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;

  4. struct Student
  5. {
  6.     char num[11];       //学号,最多10个字符
  7.     char name[11];      //姓名, 最多10个字符
  8.     int s1, s2, s3;     //三门课的考试成绩
  9.     int total;          //总成绩
  10. };

  11. int main()
  12. {
  13.     int n;
  14.     cin >> n;
  15.     Student stu[n];
  16.     for (int i = 0; i < n; i++)
  17.     {
  18.         cin >> stu[ i].num >> stu[ i].name >> stu[ i].s1 >> stu[ i].s2 >> stu[ i].s3;
  19.         stu[ i].total = stu[ i].s1 + stu[ i].s2 + stu[ i].s3;
  20.     }
  21.     int maxTotal = -1;
  22.     for (int i = 0; i < n; i++)
  23.     {
  24.         if (stu[ i].total > maxTotal)
  25.         {
  26.             maxTotal = stu[ i].total;
  27.         }
  28.     }
  29.     for (int i = 0; i < n; i++)
  30.     {
  31.         if (stu[ i].total == maxTotal)
  32.         {
  33.             cout << stu[ i].num << " " << stu[ i].name << " " << stu[ i].total << endl;
  34.         }
  35.     }
  36.     return 0;
  37. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-21 14:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表