# 自己找不出问题，求解

lb1992209760 2017-11-07 10:47:26

#include<stdio.h>
struct Stu{
char a[10];
char name[10];
int grade1;
int grade2;
int grade3;
int sum;
};
int main()
{
struct Stu stu[100];
int i,N,max,maxi;
scanf("%d\n",&N);
for(i=0;i<N;i++){
scanf("%s %s %d %d %d\n",&stu[i].a,&stu[i].name,&stu[i].grade1,&stu[i].grade2,&stu[i].grade3);
stu[i].sum=stu[i].grade1+stu[i].grade2+stu[i].grade3;
}
max=stu[0].sum;
for(i=1;i<N;i++){
if(max<stu[i].sum){
maxi=i;
max=stu[i].sum;
}
}
printf("%s %s %d",stu[maxi].name,stu[maxi].a,stu[maxi].sum);
return 0;
}
...全文
98 3 点赞 打赏 收藏 举报

3 条回复

#include<stdio.h>

struct Stu {
char a[10];
char name[10];
int grade1;
int grade2;
int grade3;
int sum;
};

int get_max_idx(struct Stu *arr, int len);

int main()
{
static struct Stu stu[100];
int i,N,max,maxi;
scanf("%d",&N);
for(i=0;i<N;i++){
scanf("%s %s %d %d %d", stu[i].a, stu[i].name,&stu[i].grade1,&stu[i].grade2,&stu[i].grade3);
stu[i].sum=stu[i].grade1+stu[i].grade2+stu[i].grade3;
}
#if 1
maxi = get_max_idx(stu, N);
#else
max=stu[0].sum;
for(i=1;i<N;i++){
if(max<stu[i].sum){
maxi=i;
max=stu[i].sum;
}
}
#endif
printf("%s %s %d",stu[maxi].name,stu[maxi].a,stu[maxi].sum);
return 0;
}

int get_max_idx(struct Stu *arr, int len)
{
int i;
int max_i = 0;

for (i = 1; i < len; i++)
if (arr[i].sum > arr[max_i].sum)
max_i = i;

return max_i;

}

scanf("%s %s %d %d %d", stu[i].a, stu[i].name,&stu[i].grade1,&stu[i].grade2,&stu[i].grade3);
• 打赏
• 举报

FoolCarpe 2017-11-08
scanf去掉‘\n’
• 打赏
• 举报

Intel0011 2017-11-08
scanf("%d\n",&N); --> scanf("%d",&N); scanf("%s %s %d %d %d\n",&stu[i].a,&stu[i].name,&stu[i].grade1,&stu[i].grade2,&stu[i].grade3); --> scanf("%s %s %d %d %d",stu[i].a,stu[i].name,&stu[i].grade1,&stu[i].grade2,&stu[i].grade3); //数组不需加&
• 打赏
• 举报

C语言

6.5w+

C语言相关问题讨论

2017-11-07 10:47