poj1547---结构数组

题意:老师发给每个学生的橡皮泥相同,找出谁抢了谁的橡皮泥

思路:结构数组存储每个学生的橡皮总量,和名字

/*   结构数组存储用户信息--只放名称和体积
    while输入循环复用长宽高变量
    for循环求所有体积和
    求出平均值
    for循环遍历部分结构数组用输入的n决定次数
    遇到结构中小于平均值,将这个结构中的名字串
    付到vicim[]
    遇到大于的,将这个结构中名称付给bully[]
    printf("bully took clay from victim.
") */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct per{
    int volume;
    char name[10];
};
int main()
{
    int n,i;
    struct per classStudentInfo[9];
    char bully[10],victim[10];
    while(scanf("%d",&n)!=EOF)
    {
        int len,wid,hei,th=0,temp;
        if(n==-1)
            break;
        temp=n;
        while(n--)
        {
            scanf("%d %d %d %s",&len,&wid,&hei,classStudentInfo[th].name);
            classStudentInfo[th].volume=len*wid*hei;
            th++;
        }
        int sumOfVol=0;
        for(i=0;i<temp;i++)
        {
            sumOfVol+=classStudentInfo[i].volume;
        }
        int averOfvol=sumOfVol/temp;
        for(i=0;i<temp;i++)
        {
            if(classStudentInfo[i].volume>averOfvol)
                strcpy(bully,classStudentInfo[i].name);
            if(classStudentInfo[i].volume<averOfvol)
                strcpy(victim,classStudentInfo[i].name);
        }
        printf("%s took clay from %s.
",bully,victim);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/gabygoole/p/4486138.html