/home/toolbox/public_html/solutions/1/119/a.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdint.h>
7 #include <math.h>
8 #include <stdlib.h>
9 #include <ctype.h>
10
11 #define TRUE (1 == 1)
12 #define FALSE (1 != 1)
13
14 #define DEBUG if (FALSE)
15
16 #define MAX_LINE 257
17
18 /*
19 * Author: Isaac Traxler
20 * Date: 2021-11-18
21 * Purpose: fun
22 * Problem: 119 - Greedy Gift Givers
23 */
24
25 /*
26 * This template reads lines of data at a time until end of file.
27 */
28
29 #define MAX_NAME_LENGTH 15
30 #define MAX_PEOPLE 12
31
32 char people[MAX_PEOPLE][MAX_NAME_LENGTH];
33 int peopleCnt;
34 int accepted[MAX_PEOPLE];
35 int given[MAX_PEOPLE];
36 int taken[MAX_PEOPLE];
37 int firstGroup = TRUE;
38
39 void init()
40 {
41 /* FUNCTION init */
42 } /* FUNCTION init */
43
44 void dump()
45 {
46 /* FUNCTION dump */
47 } /* FUNCTION dump */
48
49 int getInput()
50 {
51 /* FUNCTION getInput */
52 int dataReadFlag;
53 int i;
54
55 dataReadFlag = TRUE;
56 peopleCnt = 0;
57 while ((0 == peopleCnt) && dataReadFlag)
58 {
59 /* skip whitespace */
60 dataReadFlag = (1 == scanf(" %d ", &peopleCnt));
61 } /* skip whitespace */
62 if (dataReadFlag)
63 {
64 /* get name of folks */
65 for (i=0; peopleCnt>i; i++)
66 {
67 /* for each person in the group */
68 scanf(" %s ", people[i]);
69 given[i] = 0; /* no gifts given yet */
70 taken[i] = 0; /* no gifts taken yet */
71 } /* for each person in the group */
72 } /* get name of folks */
73 return (dataReadFlag);
74 } /* FUNCTION getInput */
75
76 int findName(char n[])
77 {
78 /* FUNCTION findName*/
79 int i;
80 int idx;
81
82 for (i=0; peopleCnt>i; i++)
83 {
84 /* compare names */
85 if (0 == strcmp(n, people[i]))
86 {
87 /* found match */
88 idx = i;
89 i = peopleCnt; /* bail now */
90 } /* found match */
91 } /* compare names */
92 return idx;
93 } /* FUNCTION findName*/
94
95 void process()
96 {
97 /* FUNCTION process */
98 int i;
99 int j;
100 int cnt;
101 int giver;
102 int taker;
103 int gift;
104 int giftAmount;
105 char name[MAX_NAME_LENGTH];
106
107 /* figure out gift values */
108 for (i=0; peopleCnt>i; i++)
109 {
110 /* do each persons gift */
111 scanf(" %s ", name);
112 giver = findName(name);
113 scanf(" %d %d ", &gift, &cnt);
114 /* add initial give balance to taken */
115 given[giver] = given[giver] + gift;
116 taken[giver] = taken[giver] + gift;
117 if (0 < cnt)
118 {
119 /* not a cheapskate */
120 giftAmount = gift / cnt;
121 for (j=0; cnt>j; j++)
122 {
123 /* for each gift taker */
124 scanf(" %s ", name);
125 taker = findName(name);
126 taken[giver] = taken[giver] - giftAmount; /* take gift amount from giver */
127 taken[taker] = taken[taker] + giftAmount; /* give gift amount to taker */
128 } /* for each gift taker */
129 } /* not a cheapskate */
130 } /* do each persons gift */
131 /* print out results */
132 for (i=0; peopleCnt>i; i++)
133 {
134 /* dump each person */
135 printf("%s %d\n", people[i], (taken[i] - given[i]));
136 } /* dump each person */
137 } /* FUNCTION process */
138
139 int main()
140 {
141 /* main */
142 int moreToDo;
143
144 init();
145 moreToDo = getInput();
146 while (moreToDo)
147 {
148 /* while */
149 if (firstGroup)
150 {
151 firstGroup = FALSE;
152 }
153 else
154 {
155 printf("\n");
156 }
157 process();
158 moreToDo = getInput();
159 } /* while */
160
161 return EXIT_SUCCESS;
162 } /* main */
163