请问洛谷的sizeof是不能处理结构体吗?我这个应该怎么改

P1093 [NOIP2007 普及组] 奖学金

PancakeKing @ 2024-04-05 16:33:57

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int cmp(const void* a, const void* b)
{
    return *(int*)a - *(int*)b;
}

struct stu
{
    int num;
    int a;
    int n;
    int b;
    int c;
}s[300];
int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d %d %d", &s[i].a, &s[i].b, &s[i].c);
        s[i].num = s[i].a + s[i].b + s[i].c;
        s[i].n = i;
    }
    qsort(s, n, sizeof(stu), cmp);
    for (int i = n; i > n-5; i--)
    {
        printf("%d %d\n", s[i].n, s[i].num);
    }
    return 0;
}

by PancakeKing @ 2024-04-05 16:37:02

num写错了应该写成sum,代表(总分)和


by xiaoxiaoyyds @ 2024-04-05 16:41:23

@PancakeKing 用sort


by PancakeKing @ 2024-04-05 16:43:29

@xiaoxiaoyyds 不会cpp(还没学)


by PancakeKing @ 2024-04-05 16:44:26

@xiaoxiaoyyds sort和qsort有什么区别吗


by PancakeKing @ 2024-04-05 16:45:46

我用其他编译器算了这个结构体大小是20,然后直接写20能编译过了但是全红


by PancakeKing @ 2024-04-05 16:48:23

好像是比较函数的问题


by xiaoxiaoyyds @ 2024-04-05 16:54:01

@PancakeKing 没什么区别,只是sort比较好用


by xiaoxiaoyyds @ 2024-04-05 16:54:37

@PancakeKing 比较函数写错了


by xiaoxiaoyyds @ 2024-04-05 16:55:49

要比较多个成绩和学号


by youcaiyoujuan @ 2024-04-08 22:30:28

#include<iostream>
#include<algorithm>
using namespace std;
int maxt = -1,maxp,n;
struct student{
    int ch,total,num;
}a[305];
int main(){
    cin >> n;
    for(int i = 1;i <= n;i++){
        int ma,en;
        cin >> a[i].ch >> ma >> en;
        a[i].total = a[i].ch + ma + en;
        a[i].num = i;
    }
    //选择排序
    for(int j = 1;j <= 5;j++)//输出前五位
    {
        maxt = -1;
        //查找最大值 
        for(int i = j;i <= n;i++){
        if(a[i].total > maxt){//擂台算法
             maxt = a[i].total;
             maxp = i; 
        }
        else if(a[i].total == maxt){
            if(a[i].ch > a[maxp].ch) maxp = i;
            else if(a[i].ch == a[maxp].ch)
            if(a[i].num < a[maxp].num) maxp = i;
        }
    } 
    swap(a[j].total,a[maxp].total),swap(a[j].num,a[maxp].num);//交换
       /*
        tmp1 = a[j].total;
        a[j].total = a[maxp].total;
        a[maxp].total = tmp1;
        tmp2 = a[j].num;
        a[j].num = a[maxp].num;
        a[maxp].num = tmp2;
        */
    }
    for(int i = 1;i <= 5;i++) cout << a[i].num << " " << a[i].total << endl;
    return 0;
}
/*
int N;
struct student{
    int num,ch,total;
}a[305];
bool cmp(student a,student b){
    if(a.total != b.total) return a.total > b.total;
    else if(a.ch != b.ch) return a.ch > b.ch;
    else return a.num < b.num;
}
int main()
{
    cin >> N;
    for(int i = 0;i < N;i++){
        int ma,en;
        cin >> a[i].ch >> ma >> en;
        a[i].total = a[i].ch + ma + en;
        a[i].num = i + 1;
    }
    sort(a,a + N,cmp);
    for(int i = 0;i < 5;i++) cout << a[i].num << " " << a[i].total << endl;
    return 0;
}
*/

|