zoukankan      html  css  js  c++  java
  • Luogu 1093

    题目链接:https://www.luogu.org/problemnew/show/P1093

    题目描述
    某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前5名学生发奖学金。期末,每个学生都有3门课的成绩:语文、数学、英语。先按总分从高到低排序,如果两个同学总分相同,再按语文成绩从高到低排序,如果两个同学总分和语文成绩都相同,那么规定学号小的同学 排在前面,这样,每个学生的排序是唯一确定的。

    任务:先根据输入的3门课的成绩计算总分,然后按上述规则排序,最后按排名顺序输出前五名名学生的学号和总分。注意,在前5名同学中,每个人的奖学金都不相同,因此,你必须严格按上述规则排序。例如,在某个正确答案中,如果前两行的输出数据(每行输出两个数:学号、总分) 是:

    $7$ $279$
    $5$ $279$

    这两行数据的含义是:总分最高的两个同学的学号依次是 $7$ 号、$5$ 号。这两名同学的总分都是 $279$ (总分等于输入的语文、数学、英语三科成绩之和) ,但学号为 $7$ 的学生语文成绩更高一些。如果你的前两名的输出数据是:

    $5$ $279$
    $7$ $279$

    则按输出错误处理,不能得分。

    输入输出格式
    输入格式:
    共 $n+1$ 行。

    第 $1$ 行为一个正整数 $n( le 300)$,表示该校参加评选的学生人数。

    第 $2$ 到 $n+1$ 行,每行有 $3$ 个用空格隔开的数字,每个数字都在 $0$ 到 $100$ 之间。第 $j$ 行的 $3$ 个数字依次表示学号为 $j-1$ 的学生的语文、数学、英语的成绩。每个学生的学号按照输入顺序编号为 $1~n$(恰好是输入数据的行号减 $1$)。

    所给的数据都是正确的,不必检验。

    //感谢 黄小U饮品 修正输入格式

    输出格式:
    共 $5$ 行,每行是两个用空格隔开的正整数,依次表示前 $5$ 名学生的学号和总分。

    输入输出样例
    输入样例#1:
    6
    90 67 80
    87 66 91
    78 89 91
    88 99 77
    67 89 64
    78 89 98
    输出样例#1:
    6 265
    4 264
    3 258
    2 244
    1 237

    输入样例#2:
    8
    80 89 89
    88 98 78
    90 67 80
    87 66 91
    78 89 91
    88 99 77
    67 89 64
    78 89 98
    输出样例#2:
    8 265
    2 264
    6 264
    1 258
    5 258

    题解:

    考察对结构体的排序……没啥难度。

    纯粹用来练练手写快排和归并排序。

    AC代码(快排):

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=305;
    int n;
    struct Stu{
        int id;
        int ch,ma,en;
        bool operator>(const Stu& oth)const
        {
            if(ch+ma+en==oth.ch+oth.ma+oth.en)
            {
                if(ch==oth.ch) return id<oth.id;
                else return ch>oth.ch;
            }
            else return ch+ma+en>oth.ch+oth.ma+oth.en;
        }
    }stu[maxn];
    
    void QuickSort(Stu a[],int l,int r)
    {
        int i=l, j=r;
        Stu p=a[rand()%(r-l+1)+l];
        while(i<=j)
        {
            while(a[i]>p) i++;
            while(p>a[j]) j--;
            if(i<=j) swap(a[i],a[j]), i++, j--;
        }
        if(l<j) QuickSort(a,l,j);
        if(i<r) QuickSort(a,i,r);
    }
    int main()
    {
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            stu[i].id=i;
            scanf("%d%d%d",&stu[i].ch,&stu[i].ma,&stu[i].en);
        }
        QuickSort(stu,1,n);
        for(int i=1;i<=5;i++) printf("%d %d
    ",stu[i].id,stu[i].ch+stu[i].ma+stu[i].en);
    }

    AC代码(归并排序):

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=305;
    int n;
    struct Stu{
        int id;
        int ch,ma,en;
        bool operator>(const Stu& oth)const
        {
            if(ch+ma+en==oth.ch+oth.ma+oth.en)
            {
                if(ch==oth.ch) return id<oth.id;
                else return ch>oth.ch;
            }
            else return ch+ma+en>oth.ch+oth.ma+oth.en;
        }
    }stu[maxn];
    
    Stu tmp[maxn];
    void MergeSort(Stu a[],int l,int r)
    {
        int mid=(l+r)>>1;
        if(l<mid) MergeSort(a,l,mid);
        if(mid+1<r) MergeSort(a,mid+1,r);
        int i=l, j=mid+1, k=l;
        while(i<=mid && j<=r) tmp[k++]=a[i]>a[j]?a[i++]:a[j++];
        while(i<=mid) tmp[k++]=a[i++];
        while(j<=r) tmp[k++]=a[j++];
        memcpy(&a[l],&tmp[l],(r-l+1)*sizeof(Stu));
    }
    int main()
    {
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            stu[i].id=i;
            scanf("%d%d%d",&stu[i].ch,&stu[i].ma,&stu[i].en);
        }
        MergeSort(stu,1,n);
        for(int i=1;i<=5;i++) printf("%d %d
    ",stu[i].id,stu[i].ch+stu[i].ma+stu[i].en);
    }
  • 相关阅读:
    第12组 Beta冲刺(2/5)
    第12组 Beta冲刺(1/5)
    第12组 Alpha事后诸葛亮
    第12组 Alpha冲刺(6/6)
    第12组 Alpha冲刺(5/6)
    第12组 Alpha冲刺(4/6)
    第12组 Alpha冲刺(3/6)
    第12组 Alpha冲刺(2/6)
    Why I start blogging.
    第二十章 更新和删除数据
  • 原文地址:https://www.cnblogs.com/dilthey/p/10258891.html
Copyright © 2011-2022 走看看