鱼C论坛

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

[已解决]如何对成绩进行排序啊

[复制链接]
发表于 2023-11-10 20:33:23 | 显示全部楼层 |阅读模式

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

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

x
题目描述
又到了一学期一度的考试周,老师要开始统计大家的成绩了。

从键盘上按学号输入n
个学生的数据(包括姓名,三门课的成绩),按照总成绩从高到低排序,

并输出学生的姓名、学号和总分。

输入描述
第一行一个整数n
,表示n
个学生;

接下来n
行,每行包括一个字符串,表示学生姓名,三个整数,表示三门课的成绩。

输出描述
输出n
行,表示按照总分从高到低排序后学生的信息,包括姓名、学号和总分,以空格分开。

创建了学生类;输入数据后如何按成绩给学生排序啊
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class Student
  4. {
  5.     public:
  6.     string Name;
  7.     int Id;
  8.     int a,b,c;
  9.     int sum;
  10.     Student()
  11.     {
  12.         sum=0;
  13.     }
  14.     void zong()
  15.     {
  16.         sum=a+b+c;
  17.     }
  18. };
  19. int main()
  20. {
  21.     Student s[110]
  22.     int n;
  23.     cin>>n;int i=0;
  24.     while(n--)
  25.     {
  26.         cin>>s[i].Name>>s[i].a>>s[i].b>>s[i].c;
  27.         i++;
  28.         s[i-1].Id=i;
  29.         s[i].zong();
  30.     }
  31. }
复制代码
最佳答案
2023-11-10 20:33:32
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;

  4. class Student
  5. {
  6. public:
  7.     string Name;
  8.     int Id;
  9.     int a, b, c;
  10.     int sum;
  11.     Student()
  12.     {
  13.         sum = 0;
  14.     }
  15.     void zong()
  16.     {
  17.         sum = a + b + c;
  18.     }
  19. };

  20. bool compare(Student s1, Student s2)
  21. {
  22.     return s1.sum > s2.sum;
  23. }

  24. int main()
  25. {
  26.     Student s[110];
  27.     int n;
  28.     cin >> n;
  29.     int i = 0;
  30.     while (n--)
  31.     {
  32.         cin >> s[ i].Name >> s[ i].a >> s[ i].b >> s[ i].c;
  33.         s[ i].Id = i + 1;
  34.         s[ i].zong();
  35.         i++;
  36.     }

  37.     sort(s, s + i, compare);

  38.     for (int j = 0; j < i; j++)
  39.     {
  40.         cout << s[j].Name << " " << s[j].Id << " " << s[j].sum << endl;
  41.     }
  42.     return 0;
  43. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-10 20:33:32 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;

  4. class Student
  5. {
  6. public:
  7.     string Name;
  8.     int Id;
  9.     int a, b, c;
  10.     int sum;
  11.     Student()
  12.     {
  13.         sum = 0;
  14.     }
  15.     void zong()
  16.     {
  17.         sum = a + b + c;
  18.     }
  19. };

  20. bool compare(Student s1, Student s2)
  21. {
  22.     return s1.sum > s2.sum;
  23. }

  24. int main()
  25. {
  26.     Student s[110];
  27.     int n;
  28.     cin >> n;
  29.     int i = 0;
  30.     while (n--)
  31.     {
  32.         cin >> s[ i].Name >> s[ i].a >> s[ i].b >> s[ i].c;
  33.         s[ i].Id = i + 1;
  34.         s[ i].zong();
  35.         i++;
  36.     }

  37.     sort(s, s + i, compare);

  38.     for (int j = 0; j < i; j++)
  39.     {
  40.         cout << s[j].Name << " " << s[j].Id << " " << s[j].sum << endl;
  41.     }
  42.     return 0;
  43. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 00:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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