USACO1_1 Greedy Gift Givers [ANALYSIS]

写得好像复杂了...前前后后花了一个小时,刚开始一运行就挂了...费了半天劲才发现%运算被取模的数不能是0..T_T..这个太邪恶了

 1 /*
2 ID: majieqq1
3 PROG: gift1
4 LANG: C++
5 */
6
7 #include <stdio.h>
8 #include <string.h>
9
10 int main(void)
11 {
12 freopen("gift1.in", "r", stdin);
13 freopen("gift1.out", "w", stdout);
14
15 char name[15][20];
16 int sPre[15];
17 int sFinal[15];
18 char tmpGiver[20];
19 char tmpReceiver[20];
20 int n, money, people;
21
22 memset(sPre, 0, sizeof(sPre));
23 memset(sFinal, 0, sizeof(sFinal));
24
25 scanf("%d", &n);
26
27
28 for (int i=0; i<n; i++)
29 {
30 scanf("%s", name[i]);
31 }
32
33 int now = n;
34 while (now)
35 {
36 now--;
37 scanf("%s", tmpGiver);
38 scanf("%d%d", &money, &people);
39
40 int who;//当前掏钱的人
41 for (who=0; who<n; who++)
42 {
43 if (!strcmp(name[who], tmpGiver))
44 {
45 sPre[who] = money;
46 break;
47 }
48 }
49 sFinal[who] += sPre[who];
50
51 if (!money && !people)
52 {
53 continue;
54 }
55 for (int i=0; i<people; i++)
56 {
57 scanf("%s", tmpReceiver);
58 for (int j=0; j<n; j++)
59 {
60 if (!strcmp(name[j], tmpReceiver))
61 {
62 sFinal[j] += money / people;
63 }
64 }
65 }
66
67 sFinal[who] -= (money-money%people);
68 }
69
70 for (int i=0; i<n; i++)
71 {
72 printf("%s %d\n", name[i], sFinal[i]-sPre[i]);
73 }
74
75 return 0;
76 }



posted @ 2012-02-19 16:19  漂木  阅读(223)  评论(0编辑  收藏  举报