鱼C论坛

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

[已解决]裁判测试程序

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

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

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

x
梦山高中现需要将某普通班的最优秀学生调整入理科实验班。为此,将从两个方面考察学生,一是数学和英语两门课的总分;另一个是所有四门课的总分。分别找出两科总分和全科总分的第一名,并从中决定调整人选。

输入将首先输入学生数n, (n为不超过80的正整数);接下来依次输入各位学生的学号,数学、英语、语文、理科综合成绩。学号及四科成绩均为不超过300的正整数。

输出时:第一行输出两科总分第一的学号,第二行输出四科总分第一的学号。 约定在两位学生成绩相同时,优先选择学号较小的学生;各位学生的学号均不相同。

裁判测试程序样例:
#include <iostream>
using namespace std;
const int N=80;
struct Student{
  int num;
  int score[4];
};

/* 请在这里填写答案 */

int main()
{
  int i, j, k, n;
  bool s2(const Student &, const Student &);
  bool s4(const Student &, const Student &);
  Student st[N];
   cin>>n;
   for(i=0;i<n;i++){
      cin>>st[i].num;
      for(j=0;j<4;j++) cin>>st[i].score[j];
    }
   cout<<select(st, n, s2)<<endl;
   cout<<select(st, n, s4)<<endl;
}


输入样例:
3
6 148 150 120 252
5 148 150 117 260
7 145 148 128 287
输出样例:
5
7

这题用C++怎么做
最佳答案
2023-12-21 11:32:38
这道题目给定了一些学生的学号和成绩,要求我们找出两门课总分和四门课总分的第一名学生的学号。我们可以使用结构体来存储每个学生的学号和成绩,并定义两个比较函数来进行排序。

首先,我们定义一个结构体"Student",其中包含学号(num)和成绩(score)两个成员变量。
struct Student{    int num;    int score;};
接下来,我们定义两个比较函数,分别用于比较两科总分和四科总分。其中,比较两科总分的比较函数s2按照题目要求比较两科总分,如果两个学生的两科总分不相等,则按照总分从大到小排列;如果两科总分相等,则按照学号从小到大排列。而比较四科总分的比较函数s4也是类似的逻辑。
bool s2(const Student &a, const Student &b){    if(a.score != b.score){        return a.score > b.score;    }    else{        return a.num < b.num;    }}bool s4(const Student &a, const Student &b){    if(a.score != b.score){        return a.score > b.score;    }    else{        return a.num < b.num;    }}
接下来,在主函数中,我们首先读入学生数n,然后用一个for循环依次读入每个学生的学号和成绩,并将其存储到一个数组中。

然后,我们利用sort函数对两个数组进行排序,分别按照s2和s4来比较。

最后,我们通过输出两个数组的第一个元素的学号就可以得到答案。

完整代码如下:
#include <iostream>#include <algorithm>using namespace std;const int N = 80;struct Student{    int num;    int score;};bool s2(const Student &a, const Student &b){    if(a.score != b.score){        return a.score > b.score;    }    else{        return a.num < b.num;    }}bool s4(const Student &a, const Student &b){    if(a.score != b.score){        return a.score > b.score;    }    else{        return a.num < b.num;    }}int main(){    int i, j, k, n;    Student st[N];    cin >> n;    for(i = 0; i < n; i++){        cin >> st[i].num;        int math, english, chinese, science;        cin >> math >> english >> chinese >> science;        st[i].score = math + english;    }    sort(st, st + n, s2);    cout << st[0].num << endl;    sort(st, st + n, s4);    cout << st[0].num << endl;    return 0;}
这样,我们就可以得到两科总分和四科总分的第一名的学号了!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-21 11:32:38 | 显示全部楼层    本楼为最佳答案   
这道题目给定了一些学生的学号和成绩,要求我们找出两门课总分和四门课总分的第一名学生的学号。我们可以使用结构体来存储每个学生的学号和成绩,并定义两个比较函数来进行排序。

首先,我们定义一个结构体"Student",其中包含学号(num)和成绩(score)两个成员变量。
struct Student{    int num;    int score;};
接下来,我们定义两个比较函数,分别用于比较两科总分和四科总分。其中,比较两科总分的比较函数s2按照题目要求比较两科总分,如果两个学生的两科总分不相等,则按照总分从大到小排列;如果两科总分相等,则按照学号从小到大排列。而比较四科总分的比较函数s4也是类似的逻辑。
bool s2(const Student &a, const Student &b){    if(a.score != b.score){        return a.score > b.score;    }    else{        return a.num < b.num;    }}bool s4(const Student &a, const Student &b){    if(a.score != b.score){        return a.score > b.score;    }    else{        return a.num < b.num;    }}
接下来,在主函数中,我们首先读入学生数n,然后用一个for循环依次读入每个学生的学号和成绩,并将其存储到一个数组中。

然后,我们利用sort函数对两个数组进行排序,分别按照s2和s4来比较。

最后,我们通过输出两个数组的第一个元素的学号就可以得到答案。

完整代码如下:
#include <iostream>#include <algorithm>using namespace std;const int N = 80;struct Student{    int num;    int score;};bool s2(const Student &a, const Student &b){    if(a.score != b.score){        return a.score > b.score;    }    else{        return a.num < b.num;    }}bool s4(const Student &a, const Student &b){    if(a.score != b.score){        return a.score > b.score;    }    else{        return a.num < b.num;    }}int main(){    int i, j, k, n;    Student st[N];    cin >> n;    for(i = 0; i < n; i++){        cin >> st[i].num;        int math, english, chinese, science;        cin >> math >> english >> chinese >> science;        st[i].score = math + english;    }    sort(st, st + n, s2);    cout << st[0].num << endl;    sort(st, st + n, s4);    cout << st[0].num << endl;    return 0;}
这样,我们就可以得到两科总分和四科总分的第一名的学号了!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-3 12:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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