/home/toolbox/public_html/solutions/12/1200/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 <stdlib.h>
7 #include <math.h>
8 #include <stdint.h>
9 #include <ctype.h>
10
11 #define TRUE (1 == 1)
12 #define FALSE (1 != 1)
13
14 #define DEBUG if (FALSE)
15
16 /* fprintf(stderr, "functionName: message", varslist); */
17
18 /*
19 * Author: Isaac Traxler
20 * Date: 2025-04-09
21 * Purpose: fun
22 * Problem: 1200 A DP Problem
23 */
24
25 /*
26 * This template reads data a specified number of times.
27 */
28
29 #define MAX_LINE 300
30 #define RIGHT 0
31 #define LEFT 1
32 #define TOTAL 2
33
34 int numberOfTimes;
35 char line[MAX_LINE];
36 int x[3]; /* 0 is for left side, 1 is for right side, 2 is fot total */
37 int c[3]; /* 0 is for left side, 1 is for right side, 2 is fot total */
38
39 void init()
40 {
41 /* FUNCTION init */
42 scanf("%d ", &numberOfTimes);
43 } /* FUNCTION init */
44
45 void dump()
46 {
47 /* FUNCTION dump */
48 } /* FUNCTION dump */
49
50 void getInput()
51 {
52 /* FUNCTION getInput */
53 fgets(line, MAX_LINE, stdin);
54 /* fgets gets the eoln at the endnof the string
55 * normally, it would be set to a 0 to reemove it,
56 * but in this case we will set it to a '=' so
57 * that an '=' will terminate the second ise. We
58 * will add another '=' in case we get an input line with just a constant
59 */
60 line[strlen(line)-1] = '=';
61 line[strlen(line)] = '=';
62 line[strlen(line)+1] = 0;
63 } /* FUNCTION getInput */
64
65 int parseLine(int cursor, int side)
66 {
67 /* FUNCTION parseLine */
68 int tot = 0;
69 int operation = 1;
70 int cur;
71 int digitsFound = FALSE;
72
73 cur = cursor;
74 x[side] = 0;
75 c[side] = 0;
76 while ('=' != line[cur])
77 {
78 /* process a side */
79 switch (line[cur])
80 {
81 /* switch */
82 case '0':
83 case '1':
84 case '2':
85 case '3':
86 case '4':
87 case '5':
88 case '6':
89 case '7':
90 case '8':
91 case '9':
92 digitsFound = TRUE;
93 tot = tot * 10 + line[cur] - '0';
94 break;
95 case '+':
96 if (0 != tot)
97 {
98 /* we have a constant being built */
99 tot = tot * operation;
100 c[side] = c[side] + tot;
101 tot = 0;
102 } /* we have a constant being built */
103 operation = 1;
104 digitsFound = FALSE;
105 break;
106 case '-':
107 if (0 != tot)
108 {
109 /* we have a constant being built */
110 tot = tot * operation;
111 c[side] = c[side] + tot;
112 tot = 0;
113 } /* we have a constant being built */
114 operation = -1;
115 digitsFound = FALSE;
116 break;
117 case 'x':
118 if (! digitsFound)
119 {
120 /* no leading coefficient -- assume 1 */
121 tot = 1;
122 } /* no leading coefficient -- assume 1 */
123 digitsFound = FALSE;
124 x[side] = x[side] + (operation * tot);
125 tot = 0;
126 } /* switch */
127 DEBUG printf("(cur %d) (x %d) (c %d) (tot %d) (line[%d] %c)\n", cur, x[side], c[side], tot, cur, line[cur]);
128 cur = cur + 1;
129 } /* process a side */
130 if (0 != tot)
131 {
132 /* side ended with a constant -- add it to c */
133 c[side] = c[side] + (tot * operation);
134 } /* side ended with a constant -- add it to c */
135 return cur;
136 } /* FUNCTION parseLine */
137
138 void process()
139 {
140 /* FUNCTION process */
141 int cursor;
142 int tmp;
143 int leftover;
144
145 DEBUG printf("[%s]\n", line);
146 cursor = 0;
147 cursor = 1 + parseLine(cursor, LEFT);
148 DEBUG printf(" left %d %d\n", x[LEFT], c[LEFT]);
149 cursor = parseLine(cursor, RIGHT);
150 DEBUG printf("right %d %d\n", x[RIGHT], c[RIGHT]);
151 x[TOTAL] = x[LEFT] - x[RIGHT];
152 c[TOTAL] = c[RIGHT] - c[LEFT];
153 DEBUG printf("total %d %d\n", x[TOTAL], c[TOTAL]);
154 if (0 == x[TOTAL])
155 {
156 /* Xs canceled out */
157 if (0 == c[TOTAL])
158 {
159 /* any x value works */
160 printf("IDENTITY\n");
161 } /* any x value works */
162 else
163 {
164 /* no x value works */
165 printf("IMPOSSIBLE\n");
166 } /* no x value works */
167 } /* Xs canceled out */
168 else
169 {
170 /* we have a single solution */
171 tmp = c[TOTAL] / x[TOTAL];
172 if (0 > tmp)
173 {
174 /* negative result -- make floor work */
175 leftover = c[TOTAL] % x[TOTAL];
176 if (0 != leftover)
177 {
178 tmp = tmp -1;
179 }
180 } /* negative result -- make floor work */
181 printf("%d\n", tmp);
182 } /* we have a single solution */
183
184 } /* FUNCTION process */
185
186 int main()
187 {
188 /* main */
189 int i;
190
191 init();
192 for (i=0; i<numberOfTimes; i++)
193 {
194 /* while */
195 getInput();
196 process();
197 } /* while */
198
199 return EXIT_SUCCESS;
200 } /* main */
201
202