亦行之 2024-03-11 10:08 采纳率: 34.5%
浏览 2

pat1015报错是为什么?

img


#include<stdio.h>
#include<stdlib.h>
struct student{
    int num;
    int de;
    int cai;
    int sum;
};
int cmp(const void *a,const void *b){
    struct student *s,*t;
    s=(student *)a;
    t=(student *)b;
    int fs,ft;
    fs=s->de +s->cai ;
    ft=t->de +t->cai ;
    if(fs!=ft)
    return fs-ft;
    else if(fs==ft)//总相同 德降序 
    return (s->de )-(t->de );
    else//德相同 号升序 
    return (t->num )-(s->num );
}
void print(student a[],int n){
    for(int i=0;i<n;i++){
        printf("%d %d %d\n",a[i].num,a[i].de,a[i].cai);
    } 
}
int main(){
    int n,l,h;
    int i1=0,i2=0,i3=0,i4=0;
    int print(int a[],int n);
    scanf("%d %d %d",&n,&l,&h);
    struct student st[n],st1[n],st2[n],st3[n],st4[n];
    for(int i=0;i<n;i++){
        scanf("%d %d %d",&st[i].num ,&st[i].de ,&st[i].cai );
    }
    for(int i=0;i<n;i++){
        if(st[i].de >=l&&st[i].cai >=l){
            if(st[i].de >=h&&st[i].cai >=h){
                st1[i1].de =st[i].de ;
                st1[i1].cai =st[i].cai ;
                st1[i1].sum =st[i].de +st[i].cai ;
                i1++;
            }
            else if(st[i].de >=h){
                st2[i2].de =st[i].de ;
                st2[i2].cai =st[i].cai ;
                st2[i2].sum =st[i].de +st[i].cai ;
                i2++;
            }
            else if(st[i].de >=st[i].cai ){
                st3[i3].de =st[i].de ;
                st3[i3].cai =st[i].cai ;
                st3[i3].sum =st[i].de +st[i].cai ;
                i3++;
            }
            else{
                st4[i4].de=st[i].de ;
                st4[i4].cai=st[i].cai ;
                st4[i4].sum=st[i].cai +st[i].de ;
                i4++;
            }
        }
    }
    qsort(st1,i1,sizeof *st1,cmp);
    qsort(st2,i2,sizeof *st2,cmp);
    qsort(st3,i3,sizeof *st3,cmp);
    qsort(st4,i4,sizeof *st4,cmp);
    printf("%d\n",i1+i2+i3+i4);
    print(st1,i1);
    print(st2,i2);
    print(st3,i3);
    print(st4,i4);
    return 0;
}

在dev中运行会有报错究竟是什么原因?

img

  • 写回答

4条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-03-11 10:09
    关注

    引自免费微信小程序:皆我百晓生

    在您提供的代码中,print函数的原型声明不正确且与调用时不匹配。原始声明是int print(int a[], int n);,它期望一个整数数组作为参数,但在main函数中您实际传入的是结构体数组student类型。因此,编译器报错提示不能将student*转换为int*

    要修复此问题,请首先修改print函数的声明和定义以接受struct student类型的数组:

    void print(struct student a[], int n){
        for(int i = 0; i < n; i++){
            printf("%d %d %d\n", a[i].num, a[i].de, a[i].cai);
        } 
    }
    

    然后,在main函数中调用print时去掉对print函数返回值的多余引用:

    // 修改main函数中的调用
    print(st1, i1);
    print(st2, i2);
    print(st3, i3);
    print(st4, i4);
    

    同时,由于您已使用qsort对各个考生数组进行了排序,所以可以合并四个数组后再进行一次排序,并去除不必要的i1, i2, i3, i4计数变量。下面是改进后的main函数部分:

    int main(){
        int n, l, h;
        scanf("%d %d %d", &n, &l, &h);
    
        struct student st[n];
        struct student sorted_st[n];
    
        // 输入数据并分类到st数组中...
    
        int total_count = i1 + i2 + i3 + i4;
    
        // 将四个子数组合并到sorted_st中...
        int j = 0;
        for (int k = 0; k < i1; ++k) {
            sorted_st[j++] = st1[k];
        }
        for (int k = 0; k < i2; ++k) {
            sorted_st[j++] = st2[k];
        }
        for (int k = 0; k < i3; ++k) {
            sorted_st[j++] = st3[k];
        }
        for (int k = 0; k < i4; ++k) {
            sorted_st[j++] = st4[k];
        }
    
        qsort(sorted_st, total_count, sizeof *sorted_st, cmp);
    
        printf("%d\n", total_count);
        print(sorted_st, total_count);
    
        return 0;
    }
    

    这样修改后,您的程序应该能正确编译并通过qsort对考生按照规则进行排序。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 3月11日

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表