/*
題意:
1拍嵌、給出N個(gè)數(shù)遭赂,按非遞增序列組成螺旋矩陣,左上角第一個(gè)格子横辆,順時(shí)針撇他,
m*n = N,m>=n狈蚤,而且m-n盡可能大困肩,也就是要求金坑你方陣
解題:
1、輸入N
2脆侮、求得n和m(開根號(hào))
3锌畸、然后讀入所有的元素
4、排序
5靖避、創(chuàng)建二維數(shù)組
6潭枣、遍歷 排序的終止條件都跟i有關(guān)
learn && wrong:
1、遍歷
2幻捏、level盆犁,也就是中間值的學(xué)習(xí)
3、vector指定大小篡九,括號(hào)()里面加數(shù)字
*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int cmp(int a, int b) { return a > b; } //排序
int main() {
int N, m, n, t = 0;
scanf("%d", &N);
for (n = sqrt((double)N); n >= 1; n--) { //這個(gè)不錯(cuò)谐岁,從根號(hào)開始找
if (N % n == 0) { //m行n列
m = N / n;
break;
}
}
vector<int> a(N); //初始化向量長(zhǎng)度
for (int i = 0; i < N; i++) //讀入長(zhǎng)度
scanf("%d", &a[i]);
sort(a.begin(), a.end(), cmp);
vector<vector<int> > b(m, vector<int>(n));
int level = m / 2 + m % 2; //這個(gè)特別重要,對(duì)于圖形來說,層數(shù)
for (int i = 0; i < level; i++) {
for (int j = i; j <= n - 1 - i && t <= N - 1; j++) //n
b[i][j] = a[t++];
for (int j = i + 1; j <= m - 2 - i && t <= N - 1; j++)
b[j][n - 1 - i] = a[t++];
for (int j = n - i - 1; j >= i && t <= N - 1; j--)
b[m - 1 - i][j] = a[t++];
for (int j = m - 2 - i; j >= i + 1 && t <= N - 1; j--)
b[j][i] = a[t++];
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf("%d", b[i][j]);
if (j != n - 1) printf(" ");
}
printf("\n");
}
return 0;
}