-
Notifications
You must be signed in to change notification settings - Fork 0
/
1050.c
75 lines (68 loc) · 1.32 KB
/
1050.c
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
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int cmp (const void *a, const void *b)
{
return *(int *)b - *(int *)a;
}
int main()
{
int N,i,m,n;
int a[10000];
scanf("%d",&N);
for(i = 0; i < N; i++)
{
scanf("%d",&a[i]);
}
qsort(a,N,sizeof(a[0]),cmp);
//m是大于sqrt(N)且能整除N的最小整数
for(m = 1; ;m++)
{
if(m * m >= N && N % m == 0)
{
break;
}
}
n = N / m;
// n = sqrt(N);
// while(N % n != 0)
// {
// n--;
// }
// m = N / n;
int b[m][n], mark[m][n], dx[4] = { 0, 1, 0, -1},dy[4] = {1,0,-1,0};
int di = 0,j,k = 0;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
mark[i][j] = 0;
}
}
i = j = 0;
while(k < N)
{
b[i][j] = a[k++];
mark[i][j] = 1;
int ni = i + dx[di], nj = j + dy[di];
if(ni < 0 || ni >= m || nj < 0 || nj >= n || mark[ni][nj])
{
di = (di + 1) % 4;
}
i += dx[di];
j += dy[di];
}
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
printf("%d",b[i][j]);
if(j + 1 < n)
{
printf(" ");
}
}
printf("\n");
}
return 0;
}