-
Notifications
You must be signed in to change notification settings - Fork 1
/
bop0302.cpp
200 lines (176 loc) · 5.02 KB
/
bop0302.cpp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* BOP 3.2: Telephone number and its corrsponding English word
*/
#include <cstdio>
#include <iostream>
char g_lookupTable[10][10] =
{
"", // 0
"", // 1
"ABC", // 2
"DEF", // 3
"GHI", // 4
"JKL", // 5
"MNO", // 6
"PQRS", // 7
"TUV", // 8
"WXYZ" // 9
};
int g_numChars[10] = {0, 0, 3, 3, 3, 3, 3, 4, 3, 4};
int g_digitLength = 8;
void solution1(int* telephone, int* charPos, int digitLength);
void solution2(int* telephone, int* charPos, int digitLength);
void solution1(int* telephone, int* charPos, int digitLength)
{
while (true)
{
for (int k = 0; k < digitLength; ++k)
{
printf("-----------%d%d%c,", telephone[k], charPos[k], g_lookupTable[telephone[k]][charPos[k]]);
}
std::cout << std::endl;
int k = digitLength - 1;
while (k >= 0)
{
printf("k=%d, charPos=%d, g_numChars[%d]=%d\n", k, charPos[k], telephone[k], g_numChars[telephone[k]]);
if (charPos[k] < g_numChars[telephone[k]] - 1)
{
charPos[k]++;
printf("charpos[%d]++ = %d, break\n", k, charPos[k]);
break;
}
else
{
charPos[k] = 0;
k--;
printf("charpos[%d] = %d, k-- = %d\n", k+1, charPos[k+1], k);
}
}
if (k < 0)
{
break;
}
}
}
void printTelephone(int* telephone, int* charPos, int digitLength)
{
for (int k = 0; k < digitLength; ++k)
{
printf("%d%d%c,", telephone[k], charPos[k], g_lookupTable[telephone[k]][charPos[k]]);
}
printf("\n");
}
void solution2(int* telephone, int* charPos, int digitLength)
{
for (;;)
{
printTelephone(telephone, charPos, digitLength);
// adjust cursors
int k = 0;
for (;;)
{
// 当前要输出的数字在范围内,because we have printed telephone before this cursor moves, should use (N-1), instead of N.
if (charPos[k] < g_numChars[telephone[k]] - 1)
{
charPos[k]++;
break;
}
else // 本次输出已经完成,重置当前charpos游标,并移动next digit的游标。
{
charPos[k] = 0;
k++;
if (k >= digitLength)
{
break;
}
}
}
// when adjust all the cursors, quit loop.
if (k >= digitLength)
{
break;
}
}
// for (int i=0; i<digitLength; ++i)
// {
// //int number = telephone[i];
// //std::cout << number << ":" << std::endl;
//
// for (int ii=0; ii<i; ++ii)
// {
// for (charPos[ii]=0; charPos[i]<g_numChars[telephone[i]]; ++charPos[i])
// {
//
// }
// charPos[i] = 0;
//
// }
//
//
// i=0: 123
// i=1: 123, 123.
//
// while (charPos[i] < g_numChars[number])
// {
//
// charPos[i]++;
// }
//
// {
// charPos[i] = 0;
// }
//
// //std::cout << number << "," << charPos[i] << ":" << g_lookupTable[number][charPos[i]] << ";";
//
//
//
// for (int k=0; k<g_numChars[number]; ++k)
// {
// //charPos[i] = k;
// //int pos = charPos[i];
// //std::cout << pos << "," << g_lookupTable[number][pos] << ";";
// }
// std::cout << std::endl;
// }
// break;
// }
}
void recursiveSolution(int* telephone, int* charPos, int index, int digitLength)
{
//printf("----------\n");
if (index == digitLength)
{
printTelephone(telephone, charPos, digitLength);
return;
}
for (charPos[index] = 0; charPos[index] < g_numChars[telephone[index]]; ++charPos[index])
{
//printf("index:charPos=%d:%d < %d:%d\n", index, charPos[index], telephone[index], g_numChars[telephone[index]]);
recursiveSolution(telephone, charPos, index + 1, digitLength);
}
if (telephone[index] == 0)
{
recursiveSolution(telephone, charPos, index + 1, digitLength);
}
}
#define MAX_DIGITS 10
int main(int argc, const char * argv[])
{
int digitLength = 8;
int telephone[MAX_DIGITS];
for (int i=0; i<digitLength; ++i)
{
telephone[i] = rand()%10;
}
for (int i=0; i<digitLength; ++i)
{
std::cout << telephone[i];
}
std::cout << std::endl;
int charPos[MAX_DIGITS];
memset(charPos, 0, digitLength*sizeof(int));
//solution1(telephone, charPos);
//solution2(telephone, charPos, digitLength);
recursiveSolution(telephone, charPos, 0, digitLength);
return 0;
}