鶸C语言看不出问题求大佬解答

P5740 【深基7.例9】最厉害的学生

Loving_coding @ 2023-01-13 17:32:49

#include<stdio.h>

int main() {
    int N,i,j,temp=0;
    struct student {
        char name;//姓名
        int Chinese;
        int Math;
        int English;
        int sum;
    }s[1000];
    scanf("%d", &N);
    for (i = 1; i < N; i++) {
        scanf("%s", &s[i].name);
        scanf("%d", &s[i].Chinese);
        scanf("%d", &s[i].Math);
        scanf("%d", &s[i].English);
        s[i].sum = s[i].Chinese + s[i].Math + s[i].English;
    }//循环输入
    for (j = 1; j <= N; j++) 
        for (i = 1; i <= N - 1; i++) {
            if (s[i].sum > s[i + 1].sum)
                temp = s[i].sum;
                s[i].sum = s[i + 1].sum;
                s[i + 1].sum = temp;
        }//冒泡排序
    printf("%s %d %d %d", s[1].name, s[1].Chinese,s[1].Math, s[1].English);
    return 0;
}

by InversionShadow @ 2023-01-13 17:36:58

@Loving_coding 姓名是多个字符,不能用 char


by AndyPomeloMars @ 2023-01-13 17:39:49

@Loving_coding 你的姓名应该使用char数组来表示


by MicroSun @ 2023-01-13 17:53:12

而且,以后您不要一有不会的题就发帖,先自己调试


by Loving_coding @ 2023-01-13 18:01:37

@AndyPomeloMars 好的谢谢您解答


by Loving_coding @ 2023-01-13 18:02:03

@yuandingquan1101 十分感谢您您解答


by Loving_coding @ 2023-01-13 18:03:46

@MicroSun 不好意思,我有时候看几十分钟都找不到错哪了,我以后尽量先自己再仔细看几遍,实在不懂再发帖。


by MicroSun @ 2023-01-13 18:14:16

给你调好了

看看注释

#include<bits/stdc++.h>//竞赛用万能头
using namespace std;
#define sum(a) (a.Chinese+a.Math+a.English)
//在定义宏的时候记得加括号,因为系统只是无脑替换,有时会导致顺序错误
int N;//i在循环内定义可以在循环结束时让空间及时得到释放
struct student {
    char name[10];
    int Chinese;
    int Math;
    int English;
    //没有必要加sum
}maxa,temp;
int main() {
    scanf("%d", &N);
    for (int i = 0; i < N; i++) {
        scanf("%s", temp.name);
        scanf("%d", &temp.Chinese);
        scanf("%d", &temp.Math);
        scanf("%d", &temp.English);
        if(sum(temp)>sum(maxa)||maxa.name[0]=='\0')//坑点!当数据为1 \n abc 0 0 0 时如果不加"||maxa.name[0]=='\0'"会直接输出0 0 0
            maxa=temp;//结构体的优点:整体赋值
    }//循环输入
    //有没有一种可能,这里不需要全部把信息存下来,也不需要排序,只需要知道最大?
    printf("%s %d %d %d", maxa.name, maxa.Chinese,maxa.Math, maxa.English);
    return 0;
}

by MicroSun @ 2023-01-13 18:14:45

@Loving_coding


by MicroSun @ 2023-01-13 18:16:08

同时也感谢你帮我找到一道水题


by Loving_coding @ 2023-01-13 18:53:57

@MicroSun 谢谢dl,目前初学C语言,还在慢慢摸索哈哈


|