/home/toolbox/public_html/solutions/105/10527/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
10 #define TRUE (1 == 1)
11 #define FALSE (1 != 1)
12
13 #define DEBUG if (FALSE)
14 #define DEBUG1 if (FALSE)
15 #define DEBUG2 if (FALSE)
16
17 #define MAX_DIGITS 1023
18
19 unsigned short int digits[MAX_DIGITS];
20 char buff[MAX_DIGITS];
21 int digitCount;
22 int counts[10];
23
24 /*
25 * Author:
26 * Date: 2005 07 27
27 * Purpose: practice
28 * Problem: 10527 - Persistent Numbers
29 */
30
31 /*
32 * This template reads data until a terminating value is reached.
33 */
34
35 void init()
36 {
37 /* FUNCTION init */
38 } /* FUNCTION init */
39
40 void dump()
41 {
42 /* FUNCTION dump */
43 int i;
44
45 for (i = 0; i < digitCount; i++)
46 {
47 printf("%d ", digits[i]);
48 }
49
50 printf("\n");
51 } /* FUNCTION dump */
52
53 int getInput()
54 {
55 /* FUNCTION getInput */
56 int dataReadFlag;
57 int i;
58
59 scanf("%s", buff);
60 if ('-' == buff[0])
61 {
62 dataReadFlag = FALSE;
63 }
64 else
65 {
66 dataReadFlag = TRUE;
67 for (i = 0; '\0' != buff[i]; i++)
68 {
69 digits[i] = buff[i] - '0';
70 }
71 digitCount = i;
72 }
73
74 return (dataReadFlag);
75 } /* FUNCTION getInput */
76
77 int twoCheck()
78 {
79 /* FUNCTION twoCheck */
80 return (0 == (digits[digitCount - 1] % 2));
81 } /* FUNCTION twoCheck */
82
83 int threeCheck()
84 {
85 /* FUNCTION threeCheck */
86 int bigsum = 0;
87 int i;
88
89 for (i = 0; i < digitCount; i++)
90 {
91 bigsum += digits[i];
92 DEBUG1 printf("bigsum: %d\n", bigsum);
93 }
94
95 return (0 == (bigsum % 3));
96 } /* FUNCTION threeCheck */
97
98 int fiveCheck()
99 {
100 /* FUNCTION fiveCheck */
101 return (5 == digits[digitCount - 1]);
102 } /* FUNCTION fiveCheck */
103
104 int sevenCheck()
105 {
106 /* FUNCTION sevenCheck */
107 int carry = 0;
108 int source = 0;
109 int temp;
110
111 if (7 > digits[0])
112 {
113 source = 1;
114 carry = digits[0];
115 }
116
117 while (source < digitCount)
118 {
119 temp = carry * 10 + digits[source];
120 carry = temp % 7;
121
122 source++;
123 }
124
125 return (0 == carry);
126 } /* FUNCTION sevenCheck */
127
128 void magicDiv(int factor)
129 {
130 /* FUNCTION magicDiv */
131 int carry = 0;
132 int source = 0;
133 int dest = 0;
134 int temp;
135
136 if (factor > digits[0])
137 {
138 source = 1;
139 carry = digits[0];
140 }
141
142 while (source < digitCount)
143 {
144 temp = carry * 10 + digits[source];
145 carry = temp % factor;
146 digits[dest] = temp / factor;
147
148 source++;
149 dest++;
150 }
151
152 digitCount = dest;
153 } /* FUNCTION magicDiv */
154
155 void showAnswer()
156 {
157 /* FUNCTION showAnswer */
158 int i;
159 int j;
160
161 digits[9] = counts[3] / 2;
162 counts[3] %= 2;
163
164 digits[8] = counts[2] / 3;
165 counts[2] %= 3;
166
167 digits[7] = counts[7];
168
169 if ((0 < counts[2]) && (0 < counts[3]))
170 {
171 digits[6] = 1;
172 counts[2]--;
173 counts[3]--;
174 }
175 else
176 {
177 digits[6] = 0;
178 }
179
180 digits[5] = counts[5];
181
182 digits[4] = counts[2] / 2;
183 counts[2] %= 2;
184
185 digits[3] = counts[3];
186 digits[2] = counts[2];
187
188 for (i = 2; i < 10; i++)
189 {
190 for (j = 0; j < digits[i]; j++)
191 {
192 printf("%d", i);
193 }
194 }
195
196 printf("\n");
197 } /* FUNCTION showAnswer */
198
199 void process()
200 {
201 /* FUNCTION process */
202 DEBUG printf("--- %s ---\n", buff);
203
204 if (1 == digitCount)
205 {
206 printf("1%c\n", buff[0]);
207 }
208 else
209 {
210 memset(counts, 0, sizeof(counts));
211
212 while (twoCheck())
213 {
214 counts[2]++;
215 magicDiv(2);
216 }
217 while (threeCheck())
218 {
219 counts[3]++;
220 magicDiv(3);
221 }
222 while (fiveCheck())
223 {
224 counts[5]++;
225 magicDiv(5);
226 }
227 while (sevenCheck())
228 {
229 counts[7]++;
230 magicDiv(7);
231 }
232
233 DEBUG2 printf("two: %d\n", counts[2]);
234 DEBUG2 printf("three: %d\n", counts[3]);
235 DEBUG2 printf("five: %d\n", counts[5]);
236 DEBUG2 printf("seven: %d\n", counts[7]);
237
238 if (1 < digitCount)
239 {
240 printf("There is no such number.\n");
241 }
242 else
243 {
244 DEBUG2 printf("IT WORKED!!!\n");
245
246 showAnswer();
247 }
248 }
249 } /* FUNCTION process */
250
251 int main ()
252 {
253 /* main */
254 int moreToDo;
255
256 init();
257 moreToDo = getInput();
258 while (moreToDo)
259 {
260 /* while */
261 process();
262 moreToDo = getInput();
263 } /* while */
264
265 return EXIT_SUCCESS;
266 } /* main */
267