c语言求助,,为什么全re?

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

shusanbei @ 2021-11-09 18:49:43

大佬求助,,

#include<stdio.h>
struct STU
{
    char name[10];  //名字
    int yuweng;         //语文
    int shuxue;         //数学
    int yingyu;         //英语
};
int main()
{

    int n,m[10000];
    scanf("%d",&n);
    struct STU arr[n];
    for(int i;i<n;i++)
    {
        scanf("%s %d %d %d",arr[i].name,&arr[i].yuweng,&arr[i].shuxue,&arr[i].yingyu);
        m[i]=arr[i].yuweng+arr[i].shuxue+arr[i].yingyu;
    }
    int max=0,shu;
    for(int i=0;i<n;i++)
    {
        if(max==m[i])
        {
            continue;
        }
        if(max<m[i])
        {
            max=m[i];
            shu=i;
        }
    }
    printf("%s %d %d %d",arr[shu].name,arr[shu].yuweng,arr[shu].shuxue,arr[shu].yingyu);
    return 0;
}

by DaShaber @ 2021-11-09 18:50:43


    for(int i;i<n;i++)


by Albert_van @ 2021-11-09 18:51:37

struct STU arr[n];
    for(int i=0;i<n;i++)

by shusanbei @ 2021-11-09 18:59:32

@tzc_2012_awa 没注意到,,改了但是第一个点还是re为什么啊


by ud2_ @ 2021-11-09 19:17:34

main.c: In function 'main':
main.c:33:34: warning: 'shu' may be used uninitialized in this function [-Wmaybe-uninitialized]
   33 |     printf("%s %d %d %d",arr[shu].name,arr[shu].yuweng,arr[shu].shuxue,arr[shu].yingyu);
      |                          ~~~~~~~~^~~~~
main.c:15:16: warning: 'i' may be used uninitialized in this function [-Wmaybe-uninitialized]
   15 |     for(int i;i<n;i++)
      |               ~^~

by Heliox @ 2021-11-09 19:29:58

@shusanbei arr开小了,

开大一点,不要用n,n是变量,不过这样是wa


by shusanbei @ 2021-11-09 19:34:47

@Cilivan 改了之后第一个点还是re为啥

#include<stdio.h>
struct STU
{
    char name[10];  //名字
    int yuweng;         //语文
    int shuxue;         //数学
    int yingyu;         //英语
};
int main()
{

    int n,m[10000];
    scanf("%d",&n);
    struct STU arr[11000];
    for(int i=0;i<n;i++)
    {
        scanf("%s %d %d %d",arr[i].name,&arr[i].yuweng,&arr[i].shuxue,&arr[i].yingyu);
        m[i]=arr[i].yuweng+arr[i].shuxue+arr[i].yingyu;
    }
    int max=0,shu;
    for(int i=0;i<n;i++)
    {
        if(max==m[i])
        {
            continue;
        }
        if(max<m[i])
        {
            max=m[i];
            shu=i;
        }
    }
    printf("%s %d %d %d\n",arr[shu].name,arr[shu].yuweng,arr[shu].shuxue,arr[shu].yingyu);
    return 0;
}

by DaShaber @ 2021-11-09 19:40:18

@shusanbei 不好意思刚刚给出了个错误答案qwq

建议把 max 设为 -1。否则如果全部爆零就没人第一了。


by DaShaber @ 2021-11-09 19:41:23

@shusanbei 楼上做法亲测可过


by shusanbei @ 2021-11-09 20:44:14

@tzc_2012_awa 好,过了 谢谢大佬


|