| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1130 人关注过本帖
标题:c语言结构体实在不知道错哪了o(╥﹏╥)o
只看楼主 加入收藏
言希
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2018-1-30
收藏
 问题点数:0 回复次数:7 
c语言结构体实在不知道错哪了o(╥﹏╥)o
Description
有若干个学生的信息,每个学生包括学号、姓名、C语言成绩,请找出成绩最高的学生,并输出他(她)的信息。

其中:学号由不少于11位、不超过15位数字组成;姓名不超过10个汉字,中间不含空格;C语言成绩为0-100之间的整数。

Input
测试数据有多组,每组的第一行为一个正整数n(n不超过100),表示有n个学生的信息,每个学生的信息单独占一行,学生的各数据之间由一个空格分隔。

Output
对于每组测试数据,找出成绩最高的学生,输出他(她)的信息,输出单独占一行,各数据之间由一个空格分隔。若有多个学生并列最高分,则输出第一个最高分学生的信息。

Sample Input
5
20151214001 张老三 98
20151214002 李老四 99
20151214003 王老五 99
20151214004 李麻子 98
20151214005 赵二财 92
Sample Output
20151214002 李老四 99
#include<stdio.h>
struct student
{
char num[16];
char name[12];
int score[3];
int total;

} ;
int main()
{
struct student st[100];
int i,j,sum;
int m,t;
int max;
scanf("%d",&m);
while(m--)
{
for(i=0;i<m;i++)
{
scanf("%s%s",st[i].num,st[i].name);
for(j=0;j<m;j++)
scanf("%d",&st[i].score[j]);
}
for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<3;j++)
sum=sum+st[i].score[j];
st[i].total=sum;
}
max=st[0].total;
for(i=0;i<m;i++)
{
if(st[i].total>max)
max=st[i].total;
}
t=i;
printf("%s%s",st[t].num,st[t].name);
printf("%d",st[t].total);
}

}
搜索更多相关主题的帖子: c语言 学生 信息 int for 
2018-01-30 21:09
Jeyyion_han
Rank: 5Rank: 5
等 级:职业侠客
威 望:4
帖 子:207
专家分:301
注 册:2018-1-22
收藏
得分:0 
sum=sum+st[i].score[j];  搞不懂为什么要求和,既然你是找出最高的分,求和没意义,还有你input数据的时候为什么不一次性scanf进去,搞了2次循环。我先改一改你的代码  再给你发出来。
2018-01-30 21:24
Jeyyion_han
Rank: 5Rank: 5
等 级:职业侠客
威 望:4
帖 子:207
专家分:301
注 册:2018-1-22
收藏
得分:0 
sum=sum+st[i].score[j];  搞不懂为什么要求和,既然你是找出最高的分,求和没意义,还有你input数据的时候为什么不一次性scanf进去,搞了2次循环。我先改一改你的代码  再给你发出来。
2018-01-30 21:24
Jeyyion_han
Rank: 5Rank: 5
等 级:职业侠客
威 望:4
帖 子:207
专家分:301
注 册:2018-1-22
收藏
得分:0 
#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable:4996)
struct student
{
    char num[16];
    char name[12];
    int score;
    int total = 0;     //没意义,我就不用了
};
int main()
{
    struct student st[100];
    int i, m;
    int max;
    printf("学生的总人数:");
    scanf("%d", &m);  //学生总人数
    printf("请输入学生信息\n");
    for (i = 0; i < m; i++)
    {
        scanf("%s %s %d", &st[i].num, &st[i].name, &st[i].score); //注意我不是用的  &st[i].score[j]  自己思考下
    }
    //找分数最高的
    max = st[0].score;
    for (i = 1; i < m; i++)
    {
        if (max < st[i].score)
        {
            max = st[i].score;
        }
    }
    for (i = 0; i < m; i++)
    {
        if (max == st[i].score)
        {
            printf("%s %s %d", st[i].num, st[i].name, st[i].score);
        }
    }
    getchar();
    system("pause");
    return 0;
}
自己跑一边把 ,我用的VS2017可能有点不一样,不明白的直接问
2018-01-30 21:54
Jeyyion_han
Rank: 5Rank: 5
等 级:职业侠客
威 望:4
帖 子:207
专家分:301
注 册:2018-1-22
收藏
得分:0 
#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable:4996)   //你要不是用的VS 就别加了
struct student
{
    char num[16];
    char name[12];
    int score;
    int total = 0;     //没意义,我就不用了
};
int main()
{
    struct student st[100];
    int i, m;
    int max;
    printf("学生的总人数:");
    scanf("%d", &m);  //学生总人数
    printf("请输入学生信息\n");
    for (i = 0; i < m; i++)
    {
        scanf("%s %s %d", &st[i].num, &st[i].name, &st[i].score); //注意我不是用的  &st[i].score[j]  自己思考下
    }
    //找分数最高的
    max = st[0].score;
    for (i = 1; i < m; i++)
    {
        if (max < st[i].score)
        {
            max = st[i].score;
        }
    }
    for (i = 0; i < m; i++)
    {
        if (max == st[i].score)
        {
            printf("%s %s %d", st[i].num, st[i].name, st[i].score);
        }
    }
    getchar();   //你要不是用的VS 就别加了
    system("pause");  //  //你要不是用的VS 就别加了
    return 0;
}
2018-01-30 22:04
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10544
专家分:42958
注 册:2014-5-20
收藏
得分:0 
没测试
#include<stdio.h>
struct student
{
    char num[16];
    char name[12];
    int score[3];
    int total;

} ;
int main()
{
    struct student st[100];
    int i,j,sum;
    int m,t;
    int max;
    scanf("%d",&m);
    while (getchar()!='\n');
    while(m--)
    {
        for(i=0; i<m; i++)
        {
            scanf("%s%s",st[i].num,st[i].name);
            for(j=0; j<3; j++)
                scanf("%d",&st[i].score[j]);
        }
        for(i=0; i<m; i++)
        {
            sum=0;
            for(j=0; j<3; j++)
                sum=sum+st[i].score[j];
            st[i].total=sum;
        }
        max=0;
        for(i=1; i<m; i++)
        {
            if(st[i].total>st[max].total)
                max=i;
        }
        printf("%s %s %d\n",st[max].num,st[max].name,st[max].total);
    }
}
2018-01-31 05:40
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10544
专家分:42958
注 册:2014-5-20
收藏
得分:0 
回复 6楼 吹水佬
#include<stdio.h>
struct student
{
    char num[16];
    char name[12];
    int score[3];
    int total;

} ;
int main()
{
    struct student st[100];
    int i, m, max=0;
    printf("学生人数:");
    scanf("%d",&m);
    while (getchar()!='\n');
    for(i=0; i<m; i++)
    {
        printf("学号 姓名 分数1 分数2 分数3\n");
        scanf("%s%s%d%d%d", st[i].num, st[i].name, &st[i].score[0], &st[i].score[1], &st[i].score[2]);
        st[i].total = st[i].score[0] + st[i].score[1] + st[i].score[2];
        if (st[i].total > st[max].total)
            max=i;
    }
    printf("%s %s %d\n",st[max].num,st[max].name,st[max].total);
}
2018-01-31 08:23
虫眼
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:10
帖 子:314
专家分:1121
注 册:2017-11-29
收藏
得分:0 
以下是引用言希在2018-1-30 21:09:58的发言:
你上的那个acm网站?
2018-01-31 16:31
快速回复:c语言结构体实在不知道错哪了o(╥﹏╥)o
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018282 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved