-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaycode_1.cpp
More file actions
543 lines (433 loc) · 10.3 KB
/
playcode_1.cpp
File metadata and controls
543 lines (433 loc) · 10.3 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
* playcode.cpp
*
* Copyright 2012 radiohead <radiohead@ubuntu>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
* http://blog.csdn.net/v_JULY_v/article/details/6234496
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
/*
* 1、有一个整数数组,请求出两两之差绝对值最小的值,
* 记住,只要得出最小值即可,不需要求出是哪两个数。
*
*/
int MinABSValue(vector<int> &vc)
{
int i, j;
int nTmp, nMin = 0x7FFFFFFF;
for (i = 0; i < vc.size(); ++i)
{
for (j = i + 1; j < vc.size(); ++j)
{
nTmp = abs(vc[i] - vc[j]);
if (nMin > nTmp) nMin = nTmp;
}
}
return nMin;
}
/*
*2、写一个函数,检查字符是否是整数,如果是,返回其整数值。
* (或者:怎样只用4行代码编写出一个从字符串到长整形的函数?)
*
*/
int CheckChar(char *pcStr,int nLen)
{
int i = 0;
int nNum = 0;
if (pcStr == NULL) return -1;
for (i = 0; i < nLen; ++i)
{
if (pcStr[i] < '0' || pcStr[i] > '9') return -1;
nNum = nNum * 10 + (pcStr[i] - '0');
}
return nNum;
}
/*
* 3、给出一个函数来输出一个字符串的所有排列。
*
*/
void SwapChar(char *p, char *q)
{
char c;
c = *p;
*p= *q;
*q = c;
}
void Permutation(char *pcStr, char *pcBegin)
{
char *p = NULL;
if (pcStr == NULL || pcBegin == NULL) return;
if (*pcBegin == '\0')
{
cout << pcStr << endl;
return;
}
for (p = pcBegin; *p != '\0'; ++p)
{
SwapChar(p, pcBegin);
Permutation(pcStr, pcBegin + 1);
SwapChar(p, pcBegin);
}
}
/*
* 4、(a)请编写实现malloc()内存分配函数功能一样的代码。
* (b)给出一个函数来复制两个字符串A和B。字符串A的后几个字节和字符串B的前几个字节重叠。
*
*/
void* MyMalloc(int nSize)
{
void *p = NULL;
if ((p = malloc(nSize)) == NULL) return NULL;
return p;
}
void CopyStr(char *pcStrA, char *pcStrB)
{
char *pcTmp = NULL;
pcTmp = (char *)malloc(strlen(pcStrA) + 1);
strcpy(pcTmp, pcStrA);
while (pcTmp != NULL && *pcTmp != '\0')
{
*pcStrB = *pcTmp;
++pcTmp;
++pcStrB;
}
if (pcTmp != NULL) free(pcTmp);
}
/*
* 6、怎样从顶部开始逐层打印二叉树结点数据?请编程。
*
*/
typedef struct _BTNode
{
int nData;
struct _BTNode *pLeft;
struct _BTNode *pRight;
struct _BTNode *pParant;
}BTNode, *LPBTNode, *LPBTree;
typedef struct _BTQNode
{
LPBTNode pData;
struct _BTQNode *next;
struct _BTQNode *prev;
}BTQNode, *LPBTQNode;
typedef struct _BTQueue
{
LPBTQNode pHead;
LPBTQNode pTail;
}BTQueue, *LPBTQueue;
void InitBTQueue(LPBTQueue pBTQueue)
{
pBTQueue->pHead = NULL;
pBTQueue->pTail = NULL;
}
int IsEmpty(LPBTQueue pBTQueue)
{
if (pBTQueue->pHead == NULL || pBTQueue->pTail == NULL) return 1;
return 0;
}
void EnQueue(LPBTQueue pBTQueue, LPBTNode pBTNode)
{
LPBTQNode pBTQNode = NULL;
pBTQNode = (LPBTQNode)malloc(sizeof(BTQNode));
memset(pBTQNode, 0, sizeof(BTQNode));
pBTQNode->pData = pBTNode;
pBTQNode->next = pBTQueue->pTail;
pBTQueue->pTail = pBTQNode;
if (pBTQueue->pHead == NULL) pBTQueue->pHead = pBTQueue->pTail;
}
LPBTNode DeQueue(LPBTQueue pBTQueue)
{
LPBTNode pBTNode = NULL;
LPBTQNode pBTQNode = NULL;
if (IsEmpty(pBTQueue)) return NULL;
pBTQNode = pBTQueue->pHead;
pBTQueue->pHead = pBTQNode->prev;
if (pBTQueue->pHead != NULL) pBTQueue->pHead->next = NULL;
else pBTQueue->pTail = NULL;
pBTNode = pBTQNode->pData;
free(pBTQNode);
return pBTNode;
}
void TraversalTreeByScope(LPBTree pBTree)
{
LPBTNode pBTHead, pBTNode;
BTQueue BtQueue;
pBTHead = pBTree;
InitBTQueue(&BtQueue);
EnQueue(&BtQueue, pBTHead);
while (!IsEmpty(&BtQueue))
{
pBTNode = DeQueue(&BtQueue);
cout << pBTNode->nData << " ";
if (pBTNode->pLeft != NULL) EnQueue(&BtQueue, pBTNode->pLeft);
if (pBTNode->pRight != NULL) EnQueue(&BtQueue, pBTNode->pRight);
}
}
/**
* 7、怎样把一个链表掉个顺序(也就是反序,注意链表的边界条件并考虑空链表)?
*
*/
typedef struct _LNode
{
int data;
struct _LNode *next;
}LNode, *LPLNode;
void ListReverseOrder(LPLNode pLNode)
{
LPLNode pNode = NULL, pNextNode = NULL;
if (pLNode->next == NULL) return;
pNode = pLNode->next;
pLNode->next = NULL;
while (pNode != NULL)
{
pNextNode = pNode->next;
pNode->next = pLNode->next;
pLNode->next = pNode;
pNode = pNextNode;
}
}
/**
* 8、请编写能直接实现int atoi(const char * pstr)函数功能的代码。
*
*/
int MyAtoi(const char *pcStr)
{
int num = 0, sym = 1;
if (*pcStr == '+')
{
++pcStr;
}
else if (*pcStr == '-')
{
++pcStr;
sym = -1;
}
while (*pcStr != '\0')
{
num = num * 10 + (*pcStr - '0');
++pcStr;
}
return sym * num;
}
/**
* 9.编程实现两个正整数的除法,当然不能用除法操作符。
*
*/
int div_int(const int x, const int y)
{
int left_num, result;
int multi;
left_num = x;
result = 0;
multi = 0;
while(left_num>=y)
{
multi = 1;
while(y*multi <= (left_num>>1))
multi = multi<<1;
result += multi;
left_num -= y*multi;
}
return result;
}
void devide(int val1, int val2, int& res, int &rev)
{
int maxv = max(val1, val2);
int minv = min(val1, val2);
res = 0;
rev = 0;
if(maxv == minv){
res = 1;
rev = 0;
return;
}else{
while(maxv > minv){
maxv = maxv - minv;
res += 1;
}
rev = maxv;
return;
}
}
/**
* 10、在排序数组中,找出给定数字的出现次数
*
*/
int FindNumTimes(int *pnArray, int size, int n)
{
int i, count = 0;
for (i = 0; i < size; ++i)
{
if (pnArray[i] == n) break;
}
while (i < size)
{
if (pnArray[i++] == n) ++count;
else break;
}
return count;
}
/**
* 13、设计一个算法,找出二叉树上任意两个结点的最近共同父结点。
*
*/
LPBTNode FindTheParant(LPBTree pBTree, LPBTNode pBTNode1, LPBTNode pBTNode2)
{
int i = 0, j = 0;
LPBTNode aNode1[32] = {0}, aNode2[32] = {0};
while (pBTNode1 != pBTree)
{
aNode1[i++] = pBTNode1;
pBTNode1 = pBTNode1->pParant;
}
while (pBTNode2 != pBTree)
{
aNode2[j++] = pBTNode2;
pBTNode2 = pBTNode2->pParant;
}
if (aNode1[i] != aNode2[j]) return pBTree;
while (aNode1[i] == aNode2[j]) --i, --j;
return aNode1[++i];
}
/**
* 14、一棵排序二叉树,令 f=(最大值+最小值)/2,
* 设计一个算法,找出距离f值最近、大于f值的结点。
*
*/
LPBTNode FindTheNode(LPBTree pBTree, int f)
{
/*
* int f, max, min;
* LPBTNode pBTNode;
*
pBTNode = pBTree;
while (pBTNode->pLeft != NULL) pBTNode = pBTNode->pLeft;
max = pBTNode->data;
pBTNode = pBTree;
while (pBTNode->pRight != NULL) pBTNode = pBTNode->pRight;
min = pBTNode->data;
f = (max + min)/2;
*/;
if (f >= pBTree->nData && f < pBTree->pLeft->nData) return pBTree;
else if (f < pBTree->nData && f >= pBTree->pRight->nData) return pBTree->pRight;
else if (f >= pBTree->pLeft->nData) FindTheNode(pBTree->pLeft, f);
else if (f < pBTree->pRight->nData) FindTheNode(pBTree->pRight, f);
else return NULL;
}
void PrintMatrixCircle(int **num,int sX,int sY,int eX,int eY);
//给定矩阵,给定行列,由外向内顺时针打印数字
void PrintMatrixClockwisely(int **matrix,int rows,int columns)
{
if(matrix == NULL || rows < 0 || columns < 0)
return;
int startX = 0;
int startY = 0;
int endX = rows - 1;
int endY = columns - 1;
while(1)
{
if(startX > endX && startY > endY)
break;
if(startX == endX && startY > endY)
break;
if(startX > endX && startY == endY)
break;
PrintMatrixCircle(matrix,startX,startY,endX,endY);
++startX;
++startY;
--endX;
--endY;
}
}
//对于给定矩阵,给定对角线上两点,打印这一周的元素
void PrintMatrixCircle(int **num,int sX,int sY,int eX,int eY)
{
//只有一行的情况,直接打印,返回。
if(sX == eX)
{
for(int j = sY;j <= eY;++j)
{
cout<<*(*(num+sX)+j)<<"\t";
}
return;
}
//只有一列的情况,直接打印,返回。
if(sY == eY)
{
for(int i = sX;i <= eX;++i)
{
cout<<*(*(num+i)+sY)<<"\t";
}
return;
}
//一般的情况打印四行
for(int p = sY;p < eY;++p)
{
cout<<*(*(num+sX)+p)<<"\t";
}
for(int q = sX;q < eX;++q)
{
cout<<*(*(num+q)+eY)<<"\t";
}
for(int m = eY;m > sY;--m)
{
cout<<*(*(num+eX)+m)<<"\t";
}
for(int n = eX;n > sX;--n)
{
cout<<*(*(num+n)+sY)<<"\t";
}
}
void knuth(int n, int m)
{
int i;
srand((unsigned int)time(0));
for (i = 0; i < n; ++i)
{
if (rand()%(n - i) < m)
{
cout << i << " ";
--m;
}
}
cout << endl;
}
int main(int argc, char **argv)
{
//char acStr[] = "123";
//cout << CheckChar(acStr, strlen(acStr)) << endl;
//Permutation(acStr, acStr);
//int n = atoi("+123");
//cout << n <<endl;
int a[][5] = {{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}};
int *a1[5] = {a[0], a[1], a[2], a[3], a[4]};
int **a2 = a1;
//PrintArray(a, 5, 5);
PrintMatrixClockwisely(a2, 5, 5);
cout << endl;
//knuth(100, 30);
return 0;
}