NOIP2007奖学金(普及组T1)———结构体排

题解:本题主要考查排序(简单),我就用刚学的结构体排序来做。
代码如下:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int n,i,cmax=0,num;
struct s
{
    int c,m,e,total,num;
};s a[301];
bool com(s a,s b)
{
    if(a.total!=b.total)
        return a.total>b.total;
    else
        if(a.c!=b.c)
            return a.c>b.c;
        else
            return a.num<b.num;
}
int main()
{
    cin>>n;
    for(i=1;i<=n;i++)
    {
    a[i].num=i;
    cin>>a[i].c>>a[i].m>>a[i].e;
    a[i].total=a[i].c+a[i].m+a[i].e;
    }
    stable_sort(a+1,a+n+1,com);
    for(i=1;i<=5;i++)
    {
    cout<<a[i].num<<" "<<a[i].total<<endl;
    }
    return 0;
}
### NOIP 2007 奖学金问题的快速序解决方案 #### 题目描述 给定N名学生的成绩,按照总分从高到低列,并计算前五名学生获得的奖学金总额。 #### 解题思路 为了高效解决此问题,可以采用快速序算法对学生成绩进行降序序。之后遍历序后的数组来统计前五名的成绩之和。具体来说: - 定义结构体`Student`存储每位同学的信息,包括姓名、班级编号以及三门课的成绩。 - 使用快速序方法按总分高低对学生列表进行序[^1]。 ```cpp struct Student { string name; int id, score_chinese, score_math, score_english; bool operator<(const Student& other) const { return (score_chinese + score_math + score_english) > (other.score_chinese + other.score_math + other.score_english); } }; ``` - 实现快速序逻辑,在比较两个对象大小时依据其成员变量定义好的运算符重载规则执行操作[^2]。 ```cpp void quickSort(vector<Student>& students, int low, int high){ if(low >= high)return ; int pivotIndex = partition(students,low,high); quickSort(students,low,pivotIndex-1); quickSort(students,pivotIndex+1,high); } int partition(vector<Student> &students,int start ,int end ){ Student pivot=students[start]; while(start<end){ while((start<end)&&(pivot<pivot[end])) --end; swap(pivot,students[end]); while((start<end)&&(pivot>=students[start])) ++start; swap(pivot,students[start]); } students[start]=pivot; return start; } ``` - 对输入数据调用上述编写的快速序函数完成序过程并输出最终结果。 ```cpp #include<bits/stdc++.h> using namespace std; // ... 上述 struct 和 函数声明 ... int main(){ vector<Student> all_students(300); // 初始化最大可能的学生数量 cin >> N; // 输入实际参与评选的人数 for(int i = 0 ;i<N;i++){ cin>>all_students[i].name>>all_students[i].id>> all_students[i].score_chinese>>all_students[i].score_math>> all_students[i].score_english; } quickSort(all_students, 0,N-1 ); double sum_award = accumulate(begin(all_students), begin(all_students)+min(N,5),(double)0, [](double acc,const auto& s){return acc+(s.score_chinese+s.score_math+s.score_english)*0.01;} ); cout << fixed<<setprecision(2)<<sum_award<<"\n"; return 0; } ``` 通过这种方式能够有效地处理NOIP2007年的这个经典竞赛题目——奖学金分配问题[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值