您的位置:首页 > 其它

USACO 1.1 Greedy Gift Givers

2015-09-18 22:51 369 查看
/*
ID: bpsaman1
LANG: C
TASK: gift1
*/
#include <stdio.h>

#define PERSONS 10
#define LEN_NAME 14

int numOfPersons;
typedef struct PERSON{
char name[LEN_NAME + 1];
int monney;
}Person;
Person arrayOfPerson[PERSONS + 1];

int strCmp(char *one, char *another){
while (*one == *another){
if (*one == '\0')
return 0;
one++;
another++;
}
return (*one - *another);
}

int getPersonIndex(char *name){
int indexOfPerson;
for (indexOfPerson = 1; indexOfPerson <= numOfPersons; indexOfPerson++)
if (strCmp(name, arrayOfPerson[indexOfPerson].name) == 0)
return indexOfPerson;
return 0;
}

int main(){
FILE *fin  = fopen ("gift1.in", "r");
FILE *fout = fopen ("gift1.out", "w");

fscanf(fin, "%d", &numOfPersons);
int indexOfPerson;
for (indexOfPerson = 1; indexOfPerson <= numOfPersons; indexOfPerson++){
fscanf(fin, "%s", arrayOfPerson[indexOfPerson].name);
//别忘了数据初始化
arrayOfPerson[indexOfPerson].monney = 0;
}

for (indexOfPerson = 1; indexOfPerson <= numOfPersons; indexOfPerson++){
char personGiving[LEN_NAME + 1];
int outcome, numOfPersonsGet;
fscanf(fin, "%s %d %d", personGiving, &outcome, &numOfPersonsGet);

//别忘了考虑除数为0的特殊情况
if (numOfPersonsGet == 0)
continue;

int income = outcome / numOfPersonsGet;
arrayOfPerson[ getPersonIndex(personGiving) ].monney -= income * numOfPersonsGet;

while (numOfPersonsGet--){
char personGet[LEN_NAME + 1];
fscanf(fin, "%s", personGet);
arrayOfPerson[ getPersonIndex(personGet) ].monney += income;
}
}

for (indexOfPerson = 1; indexOfPerson <= numOfPersons; indexOfPerson++)
fprintf(fout, "%s %d\n", arrayOfPerson[indexOfPerson].name, arrayOfPerson[indexOfPerson].monney);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息