1105 Spiral Matrix (25 分)(蛇形矩阵)
作者:互联网
1105 Spiral Matrix (25 分)
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int a[100100], b[1010][1010];
int main(){
int N, n, ret = 0;
cin >> N;
for(int i = 0; i < N; ++i)
cin >> a[i];
sort(a, a + N, greater<int>());
for(n = sqrt(N); n > 0; n--)
if( N % n == 0)
break;
int m = N / n, ceng = m / 2 + m % 2;
for(int i = 0; i < ceng; ++i){
for(int j = i; j <= n - 1 - i && ret < N; ++j)
b[i][j] = a[ret++];
for(int j = i + 1; j <= m - 2 - i && ret < N; ++j)
b[j][n - 1 - i] = a[ret++];
for(int j = n - 1 - i; j >= i && ret < N; --j)
b[m - 1 - i][j] = a[ret++];
for(int j = m - 2 - i; j >= i + 1 && ret < N; --j)
b[j][i] = a[ret++];
}
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
if(j != 0)
cout << " ";
cout << b[i][j];
}
cout << endl;
}
}
标签:25,Matrix,int,Spiral,ret,++,--,include 来源: https://blog.csdn.net/weixin_46309592/article/details/121877337