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; }
相关文章推荐
- php提示Failed to write session data错误的解决方法
- PHP5.5和之前的版本empty函数的不同之处
- PHP has encountered a Stack overflow问题解决方法
- php 生成WML页面方法详解
- PHP过滤★等特殊符号的正则
- php中文字母数字验证码实现代码
- Pear DB 新手入门指南教程第1/3页
- 解析php中heredoc的使用方法
- php中运用http调用的GET和POST方法示例
- USACO 3.2.2:Stringsobits
- HTTP协议详解(真的很经典)
- twitter api 1.1 变化
- USACO 3.1 Shaping Regions
- 【解题报告】【USACO】酸奶工厂
- usaco-Calf Flac
- usaco/1.1 Your Ride Is Here
- usaco/1.1 Greedy Gift Givers
- usaco/1.1 Friday the Thirteenth
- usaco/ 1.1 Broken Necklace
- USACO/fact4 3.2.1