| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 394 人关注过本帖
标题:帮忙看一下错在哪儿了
只看楼主 加入收藏
cx6412074
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-11-7
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:2 
帮忙看一下错在哪儿了
问题描述:-1为输入截至,依次输出每组的最大和最小体积所对应的名字。
#include <stdio.h>
#include <stdlib.h>
typedef struct student{
    int length;
    int width;
    int height;
    int volume;
    char name[9];
}student;

typedef struct result{
    char *max[9];
    char *min[9];
}result;

int main(){
    int n;
    int i,j=0;
    int max_flag,min_flag;
    scanf("%d",&n);
    student stu[9];
        result res[9];
    while(n!=-1)
    {
        for(i = 0 ; i < n ; i ++)
        {
            scanf("%d%d%d%s",&stu[i].length,&stu[i].width,&stu[i].height,stu[i].name);
            stu[i].volume = stu[i].length*stu[i].width*stu[i].height;
        }
        int max = stu[0].volume;
        int min = stu[0].volume;
        for(i = 0 ; i < n ; i++)
        {
            if(stu[i].volume >= max)
            {
                max = stu[i].volume;
                max_flag = i;
            }
        }
        for(i = 0 ; i < n ; i++)
        {
            if(stu[i].volume <= min)
            {
                min = stu[i].volume;
                min_flag = i;
            }
        }
        *res[j].max = stu[max_flag].name;
        *res[j].min = stu[min_flag].name;
        //printf("%s took clay from %s\n",*res[j].max,*res[j].min);
        scanf("%d",&n);
        j++;
    }
    for(i = 0 ; i <= j ; i++)
        {
        printf("%s took clay from %s\n",*res[i].max,*res[i].min);
        }
    return 0;
}

程序结果:
3
10 10 2 jill
5 3 10 will
5 5 10 bill
4
2 4 10 cam
4 3 7 sam
8 11 1 graham
6 2 7 pam
-1

graham took clay from sam
graham took clay from cam

预设结果:
3
10 10 2 Jill
5 3 10 Will
5 5 10 Bill
4
2 4 10 Cam
4 3 7 Sam
8 11 1 Graham
6 2 7 Pam
-1

Bill took clay from Will.
Graham took clay from Cam.

怎么回事啊,求大神指导!
搜索更多相关主题的帖子: include result volume 
2014-11-07 11:33
xufan
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:6
帖 子:232
专家分:804
注 册:2008-10-20
收藏
得分:10 
修改如下:
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // add
typedef struct student{
    int length;
    int width;
    int height;
    int volume;
    char name[9];
}student;
typedef struct result{
    char max[9]; //modify
    char min[9]; //modify
}result;
int main(){
    int n;
    int i,j=0;
    int max_flag,min_flag;
    scanf("%d",&n);
    student stu[9];
    result res[9];
    while(n!=-1)
    {
        for(i = 0 ; i < n ; i ++)
        {
            scanf("%d%d%d%s",&stu[i].length,&stu[i].width,&stu[i].height,stu[i].name);
            stu[i].volume = stu[i].length*stu[i].width*stu[i].height;
        }
        int max = stu[0].volume;
        int min = stu[0].volume;
        for(i = 0 ; i < n ; i++)
        {
            if(stu[i].volume >= max)
            {
                max = stu[i].volume;
                max_flag = i;
            }
        }
        for(i = 0 ; i < n ; i++)
        {
            if(stu[i].volume <= min)
            {
                min = stu[i].volume;
                min_flag = i;
            }
        }
        //modify
        strncpy(res[j].max , stu[max_flag].name, strlen(stu[max_flag].name));
        res[j].max[strlen(stu[max_flag].name)] = '\0';
        strncpy(res[j].min , stu[min_flag].name, strlen(stu[min_flag].name));
        res[j].min[strlen(stu[min_flag].name)] = '\0';
        memset(stu, '\0', n);
        //@end
        scanf("%d",&n);
        j++;
    }
    for (i = 0; i < j; ++i) //modify i<=j to i<j
    {
        printf("%s took clay from %s\n",res[i].max,res[i].min);
    }
    return 0;
}


~~~~~~我的明天我知道~~~。
2014-11-07 14:27
cx6412074
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-11-7
收藏
得分:0 
回复 2 楼 xufan
感谢!
2014-11-10 10:34
快速回复:帮忙看一下错在哪儿了
数据加载中...
 
   



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

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