-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter5.c
More file actions
382 lines (324 loc) · 7.09 KB
/
chapter5.c
File metadata and controls
382 lines (324 loc) · 7.09 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include "simpio.h"
#include "genlib.h"
#include "strlib.h"
#include "random.h"
#include <string.h>
#include <stdio.h>
// Question 1
static long countHHanoiMoves = 0;
void MoveSingleDiskWithCount(char start, char finish)
{
countHHanoiMoves++;
printf("%c -> %c\n", start, finish);
}
void NHanoiMoves(int n, char start, char finish, char temp)
{
if (n == 1)
{
MoveSingleDiskWithCount(start, finish);
}
else
{
NHanoiMoves(n - 1, start, temp, finish);
MoveSingleDiskWithCount(start, finish);
NHanoiMoves(n - 1, temp, finish, start);
}
}
int main(void)
{
printf("start\n");
NHanoiMoves(3, 'A', 'B', 'C');
printf("%d\n", countHHanoiMoves);
printf("end\n");
return 0;
}
// Question 2
void MoveSingleDisk(char start, char finish)
{
printf("%c -> %c\n", start, finish);
}
void MoveTower0(int n, char start, char finish, char temp)
{
if (n == 0)
{
// MoveSingleDisk(start, finish);
return;
}
else
{
MoveTower0(n - 1, start, temp, finish);
MoveSingleDisk(start, finish);
MoveTower0(n - 1, temp, finish, start);
}
}
/*
函数的长度在递归为终止条件为0时几乎无变化,只是在调用时会多用一层栈。
*/
// Question 3
void RecursivePermute(string permutedStr, string unPermutedStr)
{
if (StringLength(unPermutedStr) == 0)
{
printf("%s\n", permutedStr);
}
else
{
for (int i = 0; i < StringLength(unPermutedStr); i++)
{
string character = CharToString(IthChar(unPermutedStr, i));
string unpermute = Concat(SubString(unPermutedStr, 0, i - 1), SubString(unPermutedStr, i + 1, StringLength(unPermutedStr) - 1));
RecursivePermute(Concat(permutedStr, character), unpermute);
}
}
}
// Question 4
void RecursivePermuteWithoutDuplicate(string permutedStr, string unPermutedStr, string lastChar)
{
if (StringLength(unPermutedStr) == 0)
{
printf("%s\n", permutedStr);
}
else
{
for (int i = 0; i < StringLength(unPermutedStr); i++)
{
if (FindChar(IthChar(unPermutedStr, i), SubString(unPermutedStr, 0, i - 1), 0) == -1)
{
string character = CharToString(IthChar(unPermutedStr, i));
string unpermute = Concat(SubString(unPermutedStr, 0, i - 1), SubString(unPermutedStr, i + 1, StringLength(unPermutedStr) - 1));
RecursivePermuteWithoutDuplicate(Concat(permutedStr, character), unpermute, character);
}
}
}
}
void ListPermutations(string str)
{
RecursivePermuteWithoutDuplicate("", str, "");
}
// Question 5
void StringPermute(string ret, string vec[], int len)
{
if (len == 0)
{
printf("%s\n", ret);
}
else
{
for (int i = 0; i < StringLength(vec[0]); i++)
{
string choosen = CharToString(IthChar(vec[0], i));
StringPermute(Concat(ret, choosen), vec + 1, len - 1);
}
}
}
void ListMnemonics(string str)
{
string vec[] = { "PRS", "ABC", "DEF" };
StringPermute("", vec, 3);
}
// Question 6
void ListSubsetsRecursive(string unListStr, string ret)
{
if (StringLength(unListStr) == 0)
{
printf("%s\n", ret);
}
else
{
for (int i = 0; i < StringLength(unListStr); i++)
{
string character = CharToString(IthChar(unListStr, i));
string remain = SubString(unListStr, i + 1, StringLength(unListStr) - 1);
ListSubsets(remain, Concat(ret, character));
}
if (StringCompare(ret, "") == 0)
{
printf("{}\n");
}
else
{
printf("%s\n", ret);
}
}
}
void ListSubsets(string str)
{
ListSubsetsRecursive(str, "");
}
// Question 7
void GenerateBinaryCodeRecursive(unsigned int nBits, string ret)
{
if (nBits == 0)
{
printf("%s\n", ret);
}
else
{
for (int i = 0; i < 2; i++)
{
GenerateBinaryCodeRecursive(nBits - 1, Concat(ret, IntegerToString(i)));
}
}
}
void GenerateBinaryCode(unsigned int nBits)
{
GenerateBinaryCodeRecursive(nBits, "");
}
// Question 8
void GenerateGrayCodeRecursive1(unsigned int nBits, string ret);
void GenerateGrayCodeRecursive2(unsigned int nBits, string ret);
void GenerateGrayCodeRecursive1(unsigned int nBits, string ret)
{
if (nBits == 0)
{
printf("%s\n", ret);
}
else
{
string digit0 = IntegerToString(0);
string digit1 = IntegerToString(1);
GenerateGrayCodeRecursive1(nBits - 1, Concat(ret, digit0));
GenerateGrayCodeRecursive2(nBits - 1, Concat(ret, digit1));
}
}
void GenerateGrayCodeRecursive2(unsigned int nBits, string ret)
{
if (nBits == 0)
{
printf("%s\n", ret);
}
else
{
string digit0 = IntegerToString(0);
string digit1 = IntegerToString(1);
GenerateGrayCodeRecursive1(nBits - 1, Concat(ret, digit1));
GenerateGrayCodeRecursive2(nBits - 1, Concat(ret, digit0));
}
}
void GenerateGrayCode(unsigned int nBits)
{
GenerateGrayCodeRecursive1(nBits, "");
}
// Question 9
int NumberOfPartitions(int array[], int length, int target)
{
if (target == 0)
{
return 1;
}
if (length == 0)
{
return 0;
}
else
{
//包含第一个元素的子集数
int include = NumberOfPartitions(array + 1, length - 1, target - array[0]);
//不包第一个元素的子集数
int uninclude = NumberOfPartitions(array + 1, length - 1, target);
return (include + uninclude);
}
}
// Question 10
bool isMeasurable(int target, int weights[], int nWeights)
{
if (target == 0)
{
return TRUE;
}
if (nWeights == 0)
{
return FALSE;
}
else
{
// 砝码移除
bool remove = isMeasurable(target, weights + 1, nWeights - 1);
// 与砝码放在相同一边
bool sameside = isMeasurable(target + weights[0], weights + 1, nWeights - 1);
// 与砝码放在不同一边
bool otherside = isMeasurable(target - weights[0], weights + 1, nWeights - 1);
return remove || sameside || otherside;
}
}
// Question 11
#define HalfInchTick 0.2
#define GraduationLength 0.2
void DrawGraduations(double height)
{
DrawLine(GraduationLength, 0);
double currentX = GetCurrentX();
double currentY = GetCurrentY();
DrawLine(0, height);
MovePen(currentX, currentY);
}
void DrawRuler(double height)
{
if (height <= HalfInchTick)
{
DrawGraduations(height);
}
else
{
DrawRuler(height / 2);
DrawGraduations(height);
DrawRuler(height / 2);
}
}
int main(void)
{
InitGraphics();
DrawRuler(1);
return 0;
}
// Question 12
#define PI 3.1415926535
void DrawPolarLine(double r, double theta)
{
double radians = theta / 180 * PI;
DrawLine(r * cos(radians), r * sin(radians));
}
void DrawCoastLine(double length, double theta, int order)
{
if (order == 0)
{
DrawPolarLine(length, theta);
}
else
{
DrawCoastLine(length / 3, theta, order - 1);
DrawCoastLine(length / 3, theta + 60, order - 1);
DrawCoastLine(length / 3, theta - 60, order - 1);
DrawCoastLine(length / 3, theta, order - 1);
}
}
// Question 13
#define MinLeaf 0.25
#define LengthDelta 0.15
#define ThetaDelta 20
void DrawTree(double length, double theta, bool continuedraw)
{
if (length <= MinLeaf || continuedraw == FALSE)
{
DrawPolarLine(length, theta);
}
else
{
DrawPolarLine(length, theta);
double x = GetCurrentX();
double y = GetCurrentY();
bool continuedraw = RandomChance(0.88);
MovePen(x, y);
DrawTree(length - LengthDelta, theta + ThetaDelta, continuedraw);
MovePen(x, y);
DrawTree(length - LengthDelta, theta - ThetaDelta, continuedraw);
}
}
int main(void)
{
InitGraphics();
Randomize();
MovePen(GetWindowWidth() / 2, 0.5);
DrawTree(1.2, 90, TRUE);
return 0;
}