-
Notifications
You must be signed in to change notification settings - Fork 319
/
ch-2.c
57 lines (51 loc) · 1.27 KB
/
ch-2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
/*
* See ../README.md
*/
/*
* Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file
*/
# define BUF_SIZE 32
int main (void) {
int num;
char number [BUF_SIZE + 1];
number [BUF_SIZE] = '\0';
while (scanf ("%d", &num) == 1) {
/*
* Set the string to all 0s
*/
for (int i = 0; i < BUF_SIZE; i ++) {
number [i] = '0';
}
while (num --) {
int i;
/*
* All trailing 3s will be turned into 1s
*/
for (i = BUF_SIZE - 1; i > 0 && number [i] == '3'; i --) {
number [i] = '1';
}
/*
* Increment the digit before the trailing 3s
*/
number [i] ++;
/*
* Replace any '11' by '12'
*/
for (i = 0; i < BUF_SIZE - 1; i ++) {
if (number [i] == '1' && number [i + 1] == '1') {
number [i + 1] = '2';
}
}
}
/*
* Print the number, without the leading 0s
*/
int i;
for (i = 0; i < BUF_SIZE && number [i] == '0'; i ++);
printf ("%s\n", number + i);
}
return (0);
}