4 条题解

  • 1
    #include <iostream>
    #include <algorithm>
    using namespace std;
    int n;
    struct stu{
    	int chinese,maths,english;
    	int tot,id;
    };
    stu a[310];
    bool cmp(stu A,stu B){
    	if(A.tot!=B.tot)return A.tot>B.tot;
    	else if(A.chinese!=B.chinese)return A.chinese>B.chinese;
    	else return A.id<B.id;
    }
    int main(){
    	cin>>n;
    	for(int i=1;i<=n;i++){
    		cin>>a[i].chinese>>a[i].maths>>a[i].english;
    		a[i].tot=a[i].chinese+a[i].maths+a[i].english;
    		a[i].id=i;
    	}
    	sort(a+1,a+n+1,cmp);
    	for(int i=1;i<=5;i++){
    		cout<<a[i].id<<" "<<a[i].tot<<endl;
    	}
    }
    
    • 1
      @ 2023-6-3 8:19:09

      思路分析: 首先,读入n和每个学生的三门成绩,计算每个学生的总分,将学生信息存储在一个结构体数组中,然后按照总分、语文成绩、学号的顺序进行排序,最后输出前五名学生的学号和总分即可。

      • 0
        @ 2024-3-17 20:27:49
        #include<bits/stdc++.h>
        using namespace std;
        const int N = 1e5 + 10;
        int n,k;
        struct stu
        {
        	int id;
        	int yw;
        	int sx;
        	int yy;
        	int total;
        }a[N];
        bool cmp(stu a1,stu a2)
        {
        	if(a1.total == a2.total)
        	{
        	    if(a1.yw == a2.yw)
        	    {
        	       a1.id < a2.id;
        	    }
        	    return a1.yw > a2.yw;
        	}
        	return a1.total > a2.total;
        }
        int main(){
            cin >> n >> k;
            for(int i = 1;i <= n;i++)
            {
                cin >> a[i].id >> a[i].yw >> a[i].sx >> a[i].yy;
                a[i].total = a[i].yw + a[i].sx + a[i].yy;
                a[i].id = i;
            }
            sort(a + 1,a + n + 1,cmp);
            for (int i = 1;i <= 5;i++)
                cout << a[k].id << " " << a[k].yw;
            return 0;
        }
        
        • 0
          @ 2024-3-17 18:38:27

          简约代码

          #include <iostream>//点个赞拿走 
          #include <algorithm>
          using namespace std;
          const int N = 1e5 + 10;
          int n,k;
          struct ikun{
          	int id , yu , shu , yin , to;
          }a[N];
          bool cmp(ikun a1,ikun a2){//想抄P1363的同学 。 
          	if (a1.to == a2.to)//记得删掉数学和英语。 
          	{
          		if (a1.yu == a2.yu)
          		{	
          			return a1.id < a2.id;
          		}
          		return a1.yu > a2.yu;
          	}
          	return a1.to > a2.to;
          } 
          int main(){
          	cin >> n;
          	for (int i = 1;i <= n;i++) 
          	{
          		cin >> a[i].yu >> a[i].shu >> a[i].yin;
          		a[i].to = a[i].yu + a[i].shu + a[i].yin;//总和 
          		a[i].id = i;
          		
          	}
          	sort(a+1,a+n+1,cmp);//自定义排序。 
          	for (int i = 1;i <= 5;i++)
          		cout << a[i].id << " " << a[i].to << endl;
          }
          
          • 1

          信息

          ID
          691
          时间
          1000ms
          内存
          256MiB
          难度
          5
          标签
          递交数
          211
          已通过
          81
          上传者