PTA乙级题 1050. 螺旋矩阵(25)
【题目链接】
感觉很难,改了好久,一直错两个点,直接上大佬的C++代码。
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
int func(int N) {
int i = sqrt((double)N);
while(i >= 1) {
if(N % i == 0)
return i;
i--;
}
return 1;
}
int cmp(int a, int b) {return a > b;}
int main() {
int N, m, n, t = 0;
scanf("%d", &N);
n = func(N);
m = N / n;
vector<int> a(N);
for (int i = 0; i < N; i++)
scanf("%d", &a[i]);
sort(a.begin(), a.end(), cmp);
vector<vector<int> > b(m, vector<int>(n));
int level = m / 2 + m % 2;
for (int i = 0; i < level; i++) {
for (int j = i; j <= n - 1 - i && t <= N - 1; j++) {
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;
}