测试地址:☞
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrix is filled in from the first element at the upper-left corner, then move in a clockwise spiral. The matrix has m rows and n columns, where m and n satisfy the following: m×n must be equal to N; m≥n; and m−n is the minimum of all the possible values.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N. Then the next line contains N positive integers to be filled into the spiral matrix. All the numbers are no more than 104. The numbers in a line are separated by spaces.
Output Specification:
For each test case, output the resulting matrix in m lines, each contains n numbers. There must be exactly 1 space between two adjacent numbers, and no extra space at the end of each line.
Sample Input:
12
37 76 20 98 76 42 53 95 60 81 58 93
Sample Output:
98 95 93
42 37 81
53 20 76
58 60 76
题意:将给定的 N 个正整数按非递增的顺序,填入“螺旋矩阵”;所谓“螺旋矩阵”,是指从左上角第1 个格子开始,按顺时针螺旋方向填充。要求矩阵的规模为 m 行 n 列,满足条件:m*n = N;m>=n;且 m-n 取所有可能值中的小值
思路:
由于输入的 N 个正整数没有顺序,因此需要将这 N 个正整数按递减顺序排列;
计算行数 m 和列数 n 的值:要求 m-n 取所有可能中的最小值,且 m*n=N,m>=n;如果 m=n,那么 n=根号N;如果 m>n,那么 n<根号N;综上,我们可以使 n 从根号 N 的整数部分开始,一直递减到 1,找到第一个满足 N%n == 0 的 n,m 的值为 N/n,此时 m-n 最小。
建立一个二维数组 b,行 m 列 n,填充时按层数填充,一个口字型矩阵为一层,计算螺旋矩阵的层数 level,如果行数 m 为偶数,层数为 m/2;如果 m 为奇数,层数为 m/2+1,故 level = m / 2 + m%2;
从左上角第一个格子开始,按顺时针螺旋方向填充,故外层循环 i 从 0 ~ level;内层循环有四个,分别是左上 --> 右上,右上 --> 右下,右下 --> 左下,左下 --> 左上;
注意:内层循环中还要控制 t <= N-1,因为如果螺旋矩阵中所有的元素已经都填充完毕,就不不能再重复填充.(可以找个质数(行为该质数,列为 1)模拟一下不加条件 t <= N-1 的情况,中间位置会重复进行填充)
填充完毕后,输出整个矩阵
c++代码1:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
int a[10010];
int cmp(int a, int b){ return a > b; }
int main(){
int N, m, n;
cin >> N;
for(int i = 0; i < N; i++)
cin >> a[i];
sort(a, a+N, cmp);
for(n = sqrt(N); n >= 1; n--)
if(N%n == 0){
m = N/n;
break;
}
int level, temp=0;
int b[m][n];
level = m/2 + m%2;
memset(b, 0, sizeof(b));
for(int i = 0; i < level; i++){
//左上 -- 右上
for(int j = i; j<n-i && temp<=N-1; j++){
b[i][j] = a[temp++];
}
//右上 -- 右下
for(int j = i+1; j<m-i-1 && temp<=N-1; j++){
b[j][n-i-1] = a[temp++];
}
//右下 -- 左下
for(int j = n-i-1; j>=i && temp<=N-1; j--){
b[m-i-1][j] = a[temp++];
}
//左下 -- 左上
for(int j = m-i-2; j>=i+1 && temp<=N-1; j--){
b[j][i] = a[temp++];
}
}
for(int i = 0; i < m; i++){
if(i != 0) cout << endl;
for(int j = 0; j < n; j++){
if(j != 0) cout << " ";
cout << b[i][j];
}
}
return 0;
}
c++代码2:
#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, level, temp=0;;
cin >> N;
vector<int> a(N);
for(int i = 0; i < N; i++)
cin >> a[i];
sort(a.begin(), a.end(), cmp);
for(n = sqrt(N); n >= 1; n--)
if(N%n == 0){
m = N/n;
break;
}
vector<vector<int>> b(m, vector<int> (n));
level = m/2 + m%2;
for(int i = 0; i < level; i++){
//左上 -- 右上
for(int j = i; j<n-i && temp<=N-1; j++){
b[i][j] = a[temp++];
}
//右上 -- 右下
for(int j = i+1; j<m-i-1 && temp<=N-1; j++){
b[j][n-i-1] = a[temp++];
}
//右下 -- 左下
for(int j = n-i-1; j>=i && temp<=N-1; j--){
b[m-i-1][j] = a[temp++];
}
//左下 -- 左上
for(int j = m-i-2; j>=i+1 && temp<=N-1; j--){
b[j][i] = a[temp++];
}
}
for(int i = 0; i < m; i++){
if(i != 0) cout << endl;
for(int j = 0; j < n; j++){
if(j != 0) cout << " ";
cout << b[i][j];
}
}
return 0;
}