仪陇家园分类信息网、仪陇生活网、仪陇家园网

搜索

1050 螺旋矩阵 (25 分

[复制链接]
seo 发表于 2022-5-31 13:35:01 | 显示全部楼层 |阅读模式
1050 螺旋矩阵 (25 分发布时间:2022/5/31 12:55:56
            
                                                       
                                                       
            
        
        
               
                     
本题要求将给定的 N 个正整数按非递增的顺序,填入“螺旋矩阵”。所谓“螺旋矩阵”,是指从左上角第 1 个格子开始,按顺时针螺旋方向填充。要求矩阵的规模为 m 行 n 列,满足条件:m×n 等于 N;m≥n;且 m−n 取所有可能值中的最小值。

输入格式:
输入在第 1 行中给出一个正整数 N,第 2 行给出 N 个待填充的正整数。所有数字不超过 1,相邻数字以空格分隔。

输出格式:
输出螺旋矩阵。每行 n 个数字,共 m 行。相邻数字以 1 个空格分隔,行末不得有多余空格。

输入样例:
12
37 76 20 98 76 42 53 95 60 81 58 93

输出样例:
98 95 93
42 37 81
53 20 76
58 60 76
  
  //甲级的可以通过,乙级的最后一个测试点没有通过
//运行时间问题,哪里的时间复杂度还可以在优化
#include
#include
#include
using namespace std;
const int maxn = 10010;
int matrix[maxn][maxn],A[maxn];
bool cmp(int a,int b){
    return a > b;
}
int main(){
    int N;
    scanf("%d",&N);
    for(int i = 0; i ){
        scanf("%d",&A);
    }
    sort(A,A+N,cmp);
    if(N == 1){
        printf("%d",A[0]);
        return 0;
    }
    int m = (int)ceil(sqrt(1.0*N));
    while(N % m != 0) m++;
    int n = N/m,i = 1,j = 1,now = 0; //m is line,n is column
    int U = 1,D = m,L = 1,R = n;
    while(now  N){
        while(now //forward to right
            matrix[j] = A[now++];
            j++;
        }
        while(now  D){
            matrix[j] = A[now++]; //to down
            i++;
        }
        while(now  L){  //forward to down
            matrix[j] = A[now++];
            j--;
        }
        while(now  U){        //forward to left
            matrix[j] = A[now++];
            i--;
        }
        U++;D--;
        L++;R--;
        i++;j++;
        if(now == N - 1) matrix[j] = A[now++];
   }
   for(int i = 1; i ){
        for(int j = 1; j ){
            printf("%d",matrix[j]);
            if(j " ");
            else printf("\n");
        }
   }
   return 0;
}  
  
  
  //网上的代码,暂留
#include
#include
#include
#include
#include
#include
using namespace std;
int n;
void solve(){
    int a[n];
    for(int i=0;i){
        scanf("%d",&a);
    }
    sort(a,a+n);
    int x=sqrt(n),m;
    while(n%x){
        x--;
    }
    m=n/x;
    int t[m][x];
    for(int side=0,k=n-1;side*2){
        for(int j=side;j){
            t[side][j]=a[k--];
        }
        for(int i=side+1;i){
            t[x-1-side]=a[k--];
        }
        for(int j=x-2-side;j>=side;j--){
            t[m-1-side][j]=a[k--];
        }
        if(x-1-side>side){
            for(int i=m-2-side;i>=side+1;i--){
                t[side]=a[k--];
            }
        }
    }
    for(int i=0;i){
        for(int j=0;j){
            printf("%d",t[j]);
            if(j+1x){
                printf(" ");
            }
        }
        printf("\n");
    }
}
int main(){
    scanf("%d",&n);
    solve();
    return 0;
}
  
  


转载于:https://www.cnblogs.com/wanghao-boke/p/10381031.html
               
        
        
   
            
        
        
回复

使用道具 举报

全部回复0 显示全部楼层

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

楼主

审核员

热门推荐

联系客服 关注微信 下载APP 返回顶部 返回列表