B1050 螺旋矩阵

1050 螺旋矩阵 (25 分)

本题要求将给定的 N 个正整数按非递增的顺序,填入“螺旋矩阵”。所谓“螺旋矩阵”,是指从左上角第 1 个格子开始,按顺时针螺旋方向填充。要求矩阵的规模为 m 行 n 列,满足条件:m×n 等于 N;m≥n;且 m−n 取所有可能值中的最小值。

输入格式:

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

输出格式:

输出螺旋矩阵。每行 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

注意:采用了蛇形填数的方法

//核心部分
while(num<N-1){
    	//由左往右
    	/*1、这里的x+1必须<=n,因为假如当x+1<n时,即x<n-1,也即x最大等于n-2,则最后++x后,x=n-1,这样			并没有完全把n列填完(这与列数从1开始也有关系,如果列数是从0开始,则用x+1<n)
    	  2、下面这里x+1<=n && a[y][x+1]采用x+1的原因是为了试探二维数组中下一个位置是否为0,及是否可			  以填数*/
    	//下面的几个while循环同理可推得。
        while(x+1<=n && !a[y][x+1]){ 
            a[y][++x]=c[++num];
        }
    	//由上往下
        while(y+1<=m && !a[y+1][x]){     
            a[++y][x]=c[++num];
        }
    	//由右往左
        while(x-1>0 && !a[y][x-1]){     
            a[y][--x]=c[++num];
        }
    	//由下往上
        while(y-1>0 && !a[y-1][x]){     
            a[--y][x]=c[++num];
        }
    } 

参考代码

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int a[10010][10010]={0};
bool cmp(int a, int b){
    return a>b;
}
int main(){
    int N, m, n, num=0, c[10010], x, y;
    scanf("%d", &N);
    for(int i=0;i<N;i++){
        scanf("%d", &c[i]);
    }  
    sort(c,c+N,cmp);
    for(n=sqrt(N*1.0);n>=1;n--){
        if(N%n==0){
            m=N/n;
            break;
        }
    }
    x=y=1;
    a[1][1]=c[0];
    while(num<N-1){
        while(x+1<=n && !a[y][x+1]){     //由左往右
            a[y][++x]=c[++num];
        }
        while(y+1<=m && !a[y+1][x]){     //由上往下
            a[++y][x]=c[++num];
        }
        while(x-1>0 && !a[y][x-1]){     //由右往左
            a[y][--x]=c[++num];
        }
        while(y-1>0 && !a[y-1][x]){     //由下往上
            a[--y][x]=c[++num];
        }
    } 
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            printf("%d", a[i][j]);
            if(j<n) printf(" ");
        }
        if(i<m) printf("\n");
    }  
    return 0;
}

从a[0][0]开始的版本:

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int a[10010][10010]={0};
bool cmp(int a, int b){
    return a>b;
}
int main(){
    int N, m, n, num=0, c[10010], x, y;
    scanf("%d", &N);
    for(int i=0;i<N;i++){
        scanf("%d", &c[i]);
    }  
    sort(c,c+N,cmp);
    for(n=sqrt(N*1.0);n>=1;n--){
        if(N%n==0){
            m=N/n;
            break;
        }
    }
    x=y=0;
    a[0][0]=c[0];
    while(num<N-1){
        while(x+1<n && !a[y][x+1]){     //由左往右
            a[y][++x]=c[++num];
        }
        while(y+1<m && !a[y+1][x]){     //由上往下
            a[++y][x]=c[++num];
        }
        while(x-1>=0 && !a[y][x-1]){     //由右往左
            a[y][--x]=c[++num];
        }
        while(y-1>=0 && !a[y-1][x]){     //由下往上
            a[--y][x]=c[++num];
        }
    } 
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            printf("%d", a[i][j]);
            if(j<n-1) printf(" ");
        }
        if(i<m-1) printf("\n");
    }  
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值