-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path33_SortArrayForMinNumber.cpp
64 lines (46 loc) · 1.06 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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX 101
#define MAX_NUM 10
char str_combine1[2 * MAX_NUM], str_combine2[2 * MAX_NUM];
int Compare(const void* str1, const void* str2)
{
strcpy(str_combine1, *(const char**)str1);
strcat(str_combine1, *(const char**)str2);
strcpy(str_combine2, *(const char**)str2);
strcat(str_combine2, *(const char**)str1);
return strcmp(str_combine1, str_combine2);
}
int PrintMinNumberOfArray(int *num, int length)
{
if (num == NULL || length <= 0)
return 0;
char *str_num[MAX];
for (int i = 0;i < length; i++)
{
str_num[i] = new char[MAX_NUM];
sprintf(str_num[i], "%d", num[i]);
}
qsort(str_num, length, sizeof(char *), Compare);
for (int i = 0; i < length; i++)
printf("%s", str_num[i]);
printf("\n");
for (int i = 0;i < length; i++)
delete[] str_num[i];
return 0;
}
int main(void)
{
int str[MAX], n;
while (cin >> n)
{
for(int i = 0;i < n; i++)
scanf("%d", &str[i]);
PrintMinNumberOfArray(str, n);
}
return 0;
}