超时求助

P1093 [NOIP2007 普及组] 奖学金

Main_Void_ @ 2024-03-22 22:48:25

#include <iostream>
#include<algorithm>
using namespace std ;

struct Student {
    int Num ;
    int All;
    int Chinese ;
    int Math ;
    int English;
} a[110] ;
bool cmp(Student a , Student b)
{
    if( a.All > b.All)
    return true ;
    else if( a.All < b.All)
    return false ;
    else
    {
        if( a.Chinese > b.Chinese)
        return true ;
        else if( a.Chinese < b.Chinese)
        return false ;
        else
        {
            if(a.Num < b.Num)
            return true ;
            else
            return false ;
        }
    }
}
int main() {
    cin.tie(0) ;
   ios:: sync_with_stdio(false);
    int n ;
    cin >> n ;
    for ( int i = 1 ; i <= n ; i++) {
        a[i].Num = i ;
        cin >> a[i].Chinese >> a[i].Math >> a[i].English ;
        a[i].All = a[i].Chinese + a[i].English + a[i].Math ;
    }

    sort(a+1 , a+n + 1 , cmp) ;
    for( int i = 1; i  <= 5 ; i++)
    {
        cout << a[i].Num<<  " " << a[i].All <<endl ;
    }
}

最后两个点超时求助


by ZTT1014 @ 2024-03-22 23:06:13

可以不用在比较学号大小,默认输入学号就是从小到大。


by Parker2023 @ 2024-03-23 21:20:47


#include<bits/stdc++.h>
using namespace std;
struct node{
    int a,b,c,d;
}k[10001];
bool cmp(node x, node y){
    if(x.b+x.c+x.d<y.b+y.c+y.d)return 0;
    else if(x.b+x.c+x.d>y.b+y.c+y.d)return 1;
    else if(x.b+x.c+x.d==y.b+y.c+y.d){
        if(x.b<y.b)return 0;
        else if(x.b>y.b)return 1;
        else if(x.b==y.b){
            if(x.a>y.a)return 0;
            else if(x.a<y.a)return 1;
        }
    }
}
int main(){
    int s;
    cin>>s;
    for(int i=1;i<=s;i++){
        cin>>k[i].b>>k[i].c>>k[i].d;
        k[i].a=i;
    }
    sort(k+1,k+s+1,cmp);
    for(int i=1;i<=5;i++)
        cout<<k[i].a<<' '<<k[i].b+k[i].c+k[i].d<<endl;
    return 0;
}

by bu_chi_suan @ 2024-03-29 17:19:21

#include<stdio.h>
int num[310];
typedef struct student{
    int Chinese;
    int Math;
    int English;
    int sum;
    int number;
}Stu;

int main()
{
    Stu stu[310]; 
    int n,t;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d%d%d",&stu[i].Chinese,&stu[i].Math,&stu[i].English);
        stu[i].sum=stu[i].Chinese+stu[i].Math+stu[i].English;
        stu[i].number=i+1;num[i]=i+1;
    }
    for(int j=0;j<n-1;j++)
    {
        for(int i=0;i<n-1-j;i++)
        {
            if(stu[i].sum<stu[i+1].sum)
            {
                t=stu[i].sum;stu[i].sum=stu[i+1].sum;stu[i+1].sum=t;
                t=stu[i].Chinese;stu[i].Chinese=stu[i+1].Chinese;stu[i+1].Chinese=t;
                t=stu[i].Math;stu[i].Math=stu[i+1].Math;stu[i+1].Math=t;
                t=stu[i].number;stu[i].number=stu[i+1].number;stu[i+1].number=t;
            }
            else if(stu[i].sum==stu[i+1].sum)
            {
                if(stu[i].Chinese<stu[i+1].Chinese)
                {
                    t=stu[i].sum;stu[i].sum=stu[i+1].sum;stu[i+1].sum=t;
                    t=stu[i].Chinese;stu[i].Chinese=stu[i+1].Chinese;stu[i+1].Chinese=t;
                    t=stu[i].Math;stu[i].Math=stu[i+1].Math;stu[i+1].Math=t;
                    t=stu[i].number;stu[i].number=stu[i+1].number;stu[i+1].number=t;
                }
                else if(stu[i].Chinese==stu[i+1].Chinese)
                {
                    if(stu[i].number>stu[i+1].number)
                    {
                        t=stu[i].sum;stu[i].sum=stu[i+1].sum;stu[i+1].sum=t;
                        t=stu[i].Chinese;stu[i].Chinese=stu[i+1].Chinese;stu[i+1].Chinese=t;
                        t=stu[i].Math;stu[i].Math=stu[i+1].Math;stu[i+1].Math=t;
                        t=stu[i].number;stu[i].number=stu[i+1].number;stu[i+1].number=t;
                    }
                }
            }
        }
    }

    for(int i=0;i<5;i++)
    {
        printf("%d %d\n",stu[i].number,stu[i].sum);
    }

    return 0;
}

by TODAYS @ 2024-03-31 21:21:08

函数可以简化

#include <bits/stdc++.h>
using namespace std;

struct student
{
    int yu;
    int shu;
    int ying;
    int zong;
    int i;
}si[305];

bool cmp(student a,student b)
{
    a.zong=a.yu+a.shu+a.ying;
    b.zong=b.yu+b.shu+b.ying;
    if(a.zong==b.zong)
    {
        if(a.yu!=b.yu)
        {
            return a.yu>b.yu;
        }
        else 
        {
            return a.i<b.i;
        }
    }
    return a.zong>b.zong;
}

int main()
{
    int n;
    cin >>n;
    for(int i=1;i<=n;i++)
    {
        cin >>si[i].yu>>si[i].shu>>si[i].ying;
        si[i].i=i;
    }
    sort(si+1,si+n+1,cmp);
    for(int i=1;i<=5;i++)
    {
        cout <<si[i].i<<" "<<si[i].yu+si[i].shu+si[i].ying<<endl;
    }
    return 0;
}

by Main_Void_ @ 2024-04-02 20:11:46

@ZTT1014 感谢


by Main_Void_ @ 2024-04-02 20:12:53

@Parker2023 cmp函数觉得有点繁杂了,但是谢谢大佬了


by Main_Void_ @ 2024-04-02 20:13:42

@bu_chi_suan 我觉得还是写一个cmp的函数看着能简单一点 ,谢谢了


by Main_Void_ @ 2024-04-02 20:14:20

@efdfw 这个 != 确实学到了 感谢大佬


|