#include <iostream>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
int a[10001], n, hang, lie, j, b[200][200] = {0},x=0,y=0;
bool flag = true;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a, a + n,greater<int>());
for(int i=sqrt(n);i>0;i--)
if (n%i == 0) {
lie = i;
hang = n / i;
break;
}
if (n == 1) {
cout << a[0] << endl;
return 0;
}
if (lie == 1) {
cout << a[0];
for (int i = 1; i < n; i++)
cout << endl << a[i];
}
else {
int i = 0, hang1 = hang, lie1 = lie, number = 0, j;
while (i < n) {
number++;
while (y < lie1) {
b[x][y] = a[i];
y++;
i++;
}
y--;
x++;
while (x < hang1) {
b[x][y] = a[i];
x++;
i++;
}
x--;
y--;
while (y >= number - 1) {
b[x][y] = a[i];
y--;
i++;
}
y++;
x--;
while (x >= number) {
b[x][y] = a[i];
x--;
i++;
}
x++;
y++;
hang1 -= 1;
lie1 -= 1;
}
for (int i = 0; i < hang; i++) {
for (j = 0; j < lie - 1; j++)
cout << b[i][j] << " ";
cout << b[i][j] << endl;
}
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200307130656639.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3cxNzM5MDk1Njk0Nw==,size_16,color_FFFFFF,t_70)