大神们,帮我找找哪错了

qq_34840204 2018-04-09 02:27:09
大表哥最近想去看电影, 但是他不知道和谁 去看,现在你有一份大表哥的同学录,大表 哥最近很 garry,你能帮助大表哥找出最符 合他要求的人去陪他看电影吗? PS.大表哥喜欢综合分数最高的。 综合分数= (颜值+学习成绩+性格)/3。其中大表哥不 能和男人在一起(数据输入保证有女性
#include<stdio.h>
#include<string.h>
int main()
{
struct
{
char name[20];
double score[3];
char xb;
}str[100],stu;
int n,i,j,a,b;
double s[10],t[10];
while(scanf("%d",&n)!=-1)
{
for(i=0;i<n;i++)
{
scanf("%s %c",str[i].name,&str[i].xb);
for(j=0;j<3;j++)
{
scanf("%lf",&str[i].score[j]);
}
}
for(i=0;i<n;i++)
{
s[i]=0;
for(j=0;j<3;j++)

{
s[i]+=str[i].score[j];
}
t[i]=s[i]*1.0/3;
}
for(i=0;i<10;i++)
{
a=i;
for(j=i+1;j<10;j++)
{
if(t[j]<t[a])
a=j;
}
if(i!=a)
{
stu=str[i];
str[i]=str[i+1];
str[i+1]=stu;
}
}
for(i=0;i<n;i++)
{
if(str[i].xb=='f');break;
}
printf("I choose %s,and her score is %lf",str[b].name,t[b]);
}
return 0;
}

...全文
320 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2018-04-09
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<string.h>

int main()
{
    struct
    {
        char name[20];
        double score[3];
        char xb;
    } str[100], stu;
    int n,i,j,a,b;
    double s[10],t[10];

    //while(scanf("%d",&n)!=-1)
    while(scanf("%d",&n) != EOF)
    {
        for(i=0;i<n;i++)
        {
            scanf("%s %c",str[i].name,&str[i].xb);
            for(j=0;j<3;j++)
            {
                scanf("%lf",&str[i].score[j]);
            }
        }
        for(i=0;i<n;i++)
        {
            s[i]=0;
            for(j=0;j<3;j++)

            {
                s[i]+=str[i].score[j];
            }
            t[i]=s[i]*1.0/3;
        }
        //for(i=0;i < 10;i++)
        for(i=0;i < n;i++)
        {
            a = i;
            //for(j=i+1;j<10;j++)
            for(j=i+1;j<n;j++)
            {
                if(t[j]<t[a])
                    a=j;
            }
            if(i!=a)
            {
                stu=str[i];
                str[i]=str[i+1];
                str[i+1]=stu;
            }
        }
        for(i=0;i<n;i++)
        {
            //if(str[i].xb=='f');
            if(str[i].xb=='f')
                break;
        }
        //printf("I choose %s,and her score is %lf", str[b].name,t[b]);
        printf("I choose %s,and her score is %lf", str[b].name,t[b]);
    }
    return 0;
}
问题列表: 1. while输入n是将!= -1改成!= EOF 2. 两层for循环里,将10改成n,因为若n输入的是小于10,则排序会导致错误; 3. 最后的输出,b是初始化的值,会导致str和t数组越界
wallesyoyo 2018-04-09
  • 打赏
  • 举报
回复
你给数组排序的那两个循环,循环终止条件10改成n啊,另外,凭什么大表哥不能和男人在一起,替大表哥感到不值。
赵4老师 2018-04-09
  • 打赏
  • 举报
回复
printf里面的%和变量的一一对应关系 scanf里面的%和变量以及变量前加不加&的一一对应关系 是C代码中非常容易出错的地方,而且通常编译还不出错。 所以在编译源代码之前值得专门仔细检查一遍甚至多遍。 在每个最后不带\n的printf后面加fflush(stdout); 在每个不想受接收缓冲区旧内容影响的scanf前面加rewind(stdin); 另外请检查scanf的返回值。
//请今后要用
char c;
scanf("%c",&c);
//时,都改为
char s[2];
char c;
scanf("%1s",s);
c=s[0];
//自动跳过一个或多个空格或Tab字符或回车换行,读取下一个字符。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧