水题啊~花了半天时间终于做出来了。。。。记录下。。。
/*
ID:zjxucha1
PROG:gift1
LANG:C
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#define MAXPEOPLE 10
#define NAMELEN 32
typedef struct Person Person;
struct Person {
char name[NAMELEN];
int total;
};
Person people[MAXPEOPLE];
int npeople;
Person* searchP(char *name)//查找
{
int i;
for (i=0; i<npeople; i++)
{
if (strcmp(name, people[i].name) == 0)
{
return &people[i];
}
}
}
int main(void)
{
int i, j;
char name[NAMELEN];
int cm, ng;
FILE *fin = fopen("gift1.in", "r");
FILE *fout = fopen("gift1.out","w");
fscanf(fin, "%d", &npeople);
assert(npeople <= MAXPEOPLE);
for (i=0; i<npeople; i++)
{
fscanf(fin, "%s", &people[i].name);
}
Person *giver, *receiver;
//计算
for (j=0; j<npeople; j++)
{
fscanf(fin, "%s%d%d", name, &cm, &ng);
giver = searchP(name);
for (i=0; i<ng; i++)
{
fscanf(fin, "%s", name);
receiver = searchP(name);
giver->total -= cm/ng;
receiver->total += cm/ng;
}
}
for (i=0; i<npeople; i++)
{
fprintf(fout, "%s %d\n", people[i].name, people[i].total);
}
fclose(fin);
return 0;
}