CBCHIPS 2023-05-17 09:31 采纳率: 100%
浏览 21
已结题

关于#c语言#的问题,请各位专家解答!

c语言求问,为什么最后什么都输出不出来,代码有什么问题吗

#include<stdio.h>
struct loc
{
    char c;
    int l;
    int d;
}s[1000][3];

int main()
{
    int N,i,S,h=0;
    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
        scanf("%s %d %d",&s[i][0].c,&s[i][1].l,&s[i][2].d);
    }
    S=(s[0][1].l)*(s[0][2].d);
    for(i=1;i<N;i++)
    {
        if((s[i][1].l)*(s[i][2].d)>S)
        {
            S=(s[i][1].l)*(s[i][2].d);
            h=i;
        }
    }
    printf("%s",s[h][0].c);
}

img

  • 写回答

4条回答 默认 最新

  • 深度学习客 2023-05-17 09:47
    关注

    根据您提供的代码,问题在于最后输出语句使用了%s格式化符,但是s[h][0].c是一个字符类型,应该使用%c格式化符。修改后的代码如下:

    
    #include<stdio.h>
    struct loc
    {
        char c;
        int l;
        int d;
    }s[1000][3];
     
    int main()
    {
        int N,i,S,h=0;
        scanf("%d",&N);
        for(i=0;i<N;i++)
        {
            scanf("%s %d %d",&s[i][0].c,&s[i][1].l,&s[i][2].d);
        }
        S=(s[0][1].l)*(s[0][2].d);
        for(i=1;i<N;i++)
        {
            if((s[i][1].l)*(s[i][2].d)>S)
            {
                S=(s[i][1].l)*(s[i][2].d);
                h=i;
            }
        }
        printf("%c",s[h][0].c);
        return 0;
    }
    
    

    另外,建议在scanf函数中使用%c格式化符读取字符类型的数据,而不是%s格式化符。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 5月25日
  • 已采纳回答 5月17日
  • 创建了问题 5月17日