题目链接:
https://pintia.cn/problem-sets/994805260223102976/problems/994805275146436608
AC代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int main(){
int N;
scanf("%d",&N);
int num[N+5];
for(int i=0;i<N;i++)
scanf("%d",&num[i]);
sort(num,num+N,greater<int>());
int m=0,n=0,min_=N;
for(int i=0;i<=N;i++){
for(int j=0;j<=i;j++){
if(i*j==N&&(i-j)<min_){
//在符合条件的数中寻找差最小的数,并记录下来
min_=i-j;
m=i;
n=j;
}
}
}
int screw[m][n];//用二维数组表示螺旋矩阵
memset(screw,0,sizeof(screw));
int i=0,j=0,k=0;
screw[i][j]=num[k++];//首先为了以后赋值方便,先将第一个元素赋值
while(k<N){//需要赋值N次
//n代表列,m代表行.
while(j+1<n&&!screw[i][j+1])
//,没有赋值过,
screw[i][++j]=num[k++];//往右,如果小于边界值,则赋值
while(i+1<m&&!screw[i+1][j])
//,没有赋值过,
screw[++i][j]=num[k++];//往下,如果小于边界值,则赋值
while(j-1>=0&&!screw[i][j-1])
//,没有赋值过,
screw[i][--j]=num[k++];//往左,如果小于边界值,则赋值
while(i-1>=0&&!screw[i-1][j])
//,没有赋值过,
screw[--i][j]=num[k++];//往上,如果小于边界值,则赋值
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(j!=0)
printf(" ");
printf("%d",screw[i][j]);
}
printf("\n");
}
return 0;
}
另外:关于螺旋矩阵:
1,Leetcode
[LeetCode] Spiral Matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, ncolumns), return all elements of the matrix in spiral order.
Example 1:
Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]
Example 2:
Input:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
//对于这种螺旋遍历的方法,重要的是要确定上下左右四条边的位置,那么初始化的时候,上边up就是0,
//下边down就是m-1,左边left是0,右边right是n-1。然后我们进行while循环,先遍历上边,
//将所有元素加入结果res,然后上边下移一位,如果此时上边大于下边,说明此时已经遍历完成了,
//直接break。同理对于下边,左边,右边,依次进行相对应的操作,这样就会使得坐标很有规律,
//并且不易出错,
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
if (matrix.empty() || matrix[0].empty()) return {};
int m = matrix.size(), n = matrix[0].size();
vector<int> res;
int up = 0, down = m - 1, left = 0, right = n - 1;
while (true) {
for (int j = left; j <= right; ++j) res.push_back(matrix[up][j]);
if (++up > down) break;
for (int i = up; i <= down; ++i) res.push_back(matrix[i][right]);
if (--right < left) break;
for (int j = right; j >= left; --j) res.push_back(matrix[down][j]);
if (--down < up) break;
for (int i = down; i >= up; --i) res.push_back(matrix[i][left]);
if (++left > right) break;
}
return res;
}
};
2,https://www.cnblogs.com/huashanqingzhu/p/4098564.html
看懂了再发代码,
二刷:
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
int N;
bool cmp(const int &a,const int &b){
return a>b;
}
int main(){
scanf("%d",&N);
int num[N+5]={0};
for(int i=0;i<N;i++)
scanf("%d",&num[i]);
sort(num,num+N,cmp);
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int di=0,nx,ny;
int m=0,n=0;
int i,j;
n=sqrt(N);
while(N%n!=0)
n--;
m=N/n;
int screw[m][n]={0};
// bool vis[m][n]={false};//二维数组如何初始化
//只能逐一初始化,而不是直接初始化,因为执行出错
bool vis[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
vis[i][j]=false;
}
}
int k=0;
i=0,j=0;
do{
screw[i][j]=num[k++];
vis[i][j]=true;
nx=i+dx[di];
ny=j+dy[di];
if(nx<0||ny<0||nx>=m||ny>=n||vis[nx][ny])
//if(nx<0||ny<0||nx>m||ny>n||vis[nx][ny])
di=(di+1)%4;
i=i+dx[di];
j=j+dy[di];
}while(k<N);
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(j!=0)
printf(" ");
printf("%d",screw[i][j]);
}
printf("\n");
}
return 0;
}