-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy path面试题33之把数组排成最小的树_SortArrayForMinNumber.cpp
121 lines (94 loc) · 2.79 KB
/
面试题33之把数组排成最小的树_SortArrayForMinNumber.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
// SortArrayForMinNumber.cpp : Defines the entry point for the console application.
//
// 《剑指Offer——名企面试官精讲典型编程题》代码
// 著作权所有者:何海涛
#include "stdafx.h"
#include <string>
#include <algorithm>
int compare(const void* strNumber1, const void* strNumber2);
// int型整数用十进制表示最多只有10位
const int g_MaxNumberLength = 10;
char* g_StrCombine1 = new char[g_MaxNumberLength * 2 + 1];
char* g_StrCombine2 = new char[g_MaxNumberLength * 2 + 1];
void PrintMinNumber(int* numbers, int length)
{
if(numbers == NULL || length <= 0)
return;
char** strNumbers = (char**)(new int[length]);
for(int i = 0; i < length; ++i)
{
strNumbers[i] = new char[g_MaxNumberLength + 1];
sprintf(strNumbers[i], "%d", numbers[i]);
}
qsort(strNumbers, length, sizeof(char*), compare);
for(int i = 0; i < length; ++i)
printf("%s", strNumbers[i]);
printf("\n");
for(int i = 0; i < length; ++i)
delete[] strNumbers[i];
delete[] strNumbers;
}
// 如果[strNumber1][strNumber2] > [strNumber2][strNumber1], 返回值大于0
// 如果[strNumber1][strNumber2] = [strNumber2][strNumber1], 返回值等于0
// 如果[strNumber1][strNumber2] < [strNumber2][strNumber1], 返回值小于0
int compare(const void* strNumber1, const void* strNumber2)
{
// [strNumber1][strNumber2]
strcpy(g_StrCombine1, *(const char**)strNumber1);
strcat(g_StrCombine1, *(const char**)strNumber2);
// [strNumber2][strNumber1]
strcpy(g_StrCombine2, *(const char**)strNumber2);
strcat(g_StrCombine2, *(const char**)strNumber1);
return strcmp(g_StrCombine1, g_StrCombine2);
}
// ====================测试代码====================
void Test(char* testName, int* numbers, int length, char* expectedResult)
{
if(testName != NULL)
printf("%s begins:\n", testName);
if(expectedResult != NULL)
printf("Expected result is: \t%s\n", expectedResult);
printf("Actual result is: \t");
PrintMinNumber(numbers, length);
printf("\n");
}
void Test1()
{
int numbers[] = {3, 5, 1, 4, 2};
Test("Test1", numbers, sizeof(numbers)/sizeof(int), "12345");
}
void Test2()
{
int numbers[] = {3, 32, 321};
Test("Test2", numbers, sizeof(numbers)/sizeof(int), "321323");
}
void Test3()
{
int numbers[] = {3, 323, 32123};
Test("Test3", numbers, sizeof(numbers)/sizeof(int), "321233233");
}
void Test4()
{
int numbers[] = {1, 11, 111};
Test("Test4", numbers, sizeof(numbers)/sizeof(int), "111111");
}
// 数组中只有一个数字
void Test5()
{
int numbers[] = {321};
Test("Test5", numbers, sizeof(numbers)/sizeof(int), "321");
}
void Test6()
{
Test("Test6", NULL, 0, "Don't print anything.");
}
int _tmain(int argc, _TCHAR* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
return 0;
}