求比较函数,急!

P1093 [NOIP2007 普及组] 奖学金

dandan2012 @ 2024-08-21 09:19:22

悬关悬关悬关!急急急!


by 11514zbs @ 2024-08-21 09:24:01

@dandan2012 ?


by shangyuang @ 2024-08-21 09:24:02

是sort的cmp函数吗?


by L_V_ @ 2024-08-21 09:26:24

变量=max(要比较的第一数字,第二数字)


by L_V_ @ 2024-08-21 09:27:40

用结构体+sort();


by L_V_ @ 2024-08-21 09:33:50

#include<bits/stdc++.h>
using namespace std;
struct ss{
    int a;
    char ch;
}xx[15]; 
int cmp(ss x,ss y){
    return x.a<y.a;
}
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>xx[i].ch>>xx[i].a;
    }
    sort(xx,xx+n+1,cmp);
    for(int i=1;i<=n;i++){
        cout<<xx[i].ch<<endl;
    }
    return 0;
}

by luozijie55358 @ 2024-08-24 20:37:34

bool cmp(node x,node y){
    if(x.zf>y.zf) return 1;//比较语文分数 
    if(x.zf==y.zf&&x.yw>y.yw) return 1;//如果语文分数一样,比总分 
    if(x.zf==y.zf&&x.yw==y.yw&&x.xh<y.xh) return1;//如果语文和总分都一样,比学号 
    return 0;//如果都不一样 
}

by qxx13840998428 @ 2024-08-26 08:42:03


#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    int arr[100001]={0};
    for(int i=1;i<=n;i++){
        cin>>arr[i];
    }
    sort(arr+1,arr+n+1);
    for(int i=1;i<=n;i++){
        cout<<arr[i]<<" ";
    }
}

by M_C_Yyyds @ 2024-08-28 15:38:13

#include <bits/stdc++.h>
using namespace std;
int n;
struct xinxi{
    int xue,zong,yu,shu,ying;
}a[310]; 
int cmp(xinxi one,xinxi two){
    if(one.zong==two.zong){
        if(one.yu==two.yu){
            return one.xue<two.xue;
        }
        return one.yu>two.yu;
    }
    return one.zong>two.zong;
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i].yu>>a[i].shu>>a[i].ying;
        a[i].zong=a[i].yu+a[i].shu+a[i].ying;
        a[i].xue=i+1;
    }
    sort(a,a+n,cmp);
    for(int i=0;i<5;i++)
    {
        cout<<a[i].xue<<" "<<a[i].zong<<endl;
    }
    return 0;
}

by hnxx123456 @ 2024-09-18 14:57:23

?


|