#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, a[10001], i=0, j=0, m, n, left,right,up,down;
scanf("%d", &N);
n = sqrt(N);//lie
m = N / n;//hang
for(i=0; i<N; i++)
scanf("%d", &a[i]);
qsort(a, N, sizeof(a[0]), cmp);
int next=0, count=1;
int b[101][101] = {0};
i=0,j=-1,left=0,right=n-1,up=0,down=m-1;
while(next < N)
{
while(count%4 == 1 && j<right)
{
b[i][++j] = a[next++];
}
count++;
right--;
while(count%4 == 2 && i<down)
{
//i++;
b[++i][j] = a[next++];
}
count++;
down--;
while(count%4 == 3 && j>left)
{
//j--;
b[i][--j] = a[next++];
}
count++;
left++;
up++;
while(count%4 == 0 && i>up)
{
//i--;
b[--i][j] = a[next++];
}
count++;
}
for(i=0; i<m; i++)//输出
{
printf("%d", b[i][0]);
for(j=1; j<n; j++)
{
printf(" %d", b[i][j]);
}
printf("\n");
}
}
网友评论