PAT (Basic Level) Practice (中文)- 1050 螺旋矩阵(25 分)

简介: PAT (Basic Level) Practice (中文)- 1050 螺旋矩阵(25 分)

题目链接:点击打开链接

题目大意:略。

解题思路:四个方向,每次判断当前是否为 0,表示未放置数据;如果有,说明这条路走到了尽头,需要调头,否则继续走。所以在外围多一圈包围着里面的答案,从(1,1)开始走。注意:定义 g[][] 大小的时候,需要动态分配,不能全局写死,因为防止出现 n==质数 的情况,如果数据很大,二维数组不够存,或者会越界。


AC 代码

#include<bits/stdc++.h>
#include<cmath>
#define mem(a,b) memset(a,b,sizeof a);
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll;
int a[10009], col, row, n;
void cal()
{
    int len=sqrt(n), f=0;
    for(int i=2;i<=len;i++)
        if(n%i==0) col=i, row=n/i, f=1;
    if(!f) col=1, row=n;
}
int main()
{
    scanf("%d",&n);
    cal(); col+=2, row+=2;
    int g[row][col]={0};
    // mem(g,0); // PAT 有时需要注释,否则TLE,而且原本若定义在全局就已经清空了
    for(int i=0;i<n;i++) scanf("%d",&a[i]);
    for(int j=0;j<col;j++) g[row-1][j]=g[0][j]=1;
    for(int i=0;i<row;i++) g[i][0]=g[i][col-1]=1;
    sort(a,a+n,greater<int>());
    int x=1,y=1,l=0,kase=1;
    while(l<n)
    {
        kase++;
        kase%=4;
        if(kase==1)
        {
            while(l<n && g[x][y]==0) g[x][y]=a[l++], x--;
            x++, y++;
        }
        else if(kase==2)
        {
            while(l<n && g[x][y]==0) g[x][y]=a[l++], y++;
            x++, y--;
        }
        else if(kase==3)
        {
            while(l<n && g[x][y]==0) g[x][y]=a[l++], x++;
            x--, y--;
        }
        else if(kase==0)
        {
            while(l<n && g[x][y]==0) g[x][y]=a[l++], y--;
            x--, y++;
        }
    }
    for(int i=1;i<row-1;i++)
        for(int j=1;j<col-1;j++)
            printf("%d%c",g[i][j],j==col-2?'\n':' ');
    return 0;
}
目录
相关文章
|
11月前
|
C语言 C++
PAT (Basic Level) Practice (中文)1099 性感素数(20分)
“性感素数”是指形如 (p, p+6) 这样的一对素数。之所以叫这个名字,是因为拉丁语管“六”叫“sex”(即英语的“性感”)。(原文摘自 http://mathworld.wolfram.com/SexyPrimes.html) 现给定一个整数,请你判断其是否为一个性感素数。
94 0
PAT (Basic Level) Practice (中文) 1016 部分A+B (15 分)
PAT (Basic Level) Practice (中文) 1016 部分A+B (15 分)
64 0
|
测试技术
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
76 0
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
PAT (Basic Level) Practice (中文) B1046 划拳 (15 分)
PAT (Basic Level) Practice (中文) B1046 划拳 (15 分)
59 0
|
C语言
PAT (Basic Level) Practice (中文) B1026 程序运行时间 (15 分)
PAT (Basic Level) Practice (中文) B1026 程序运行时间 (15 分)
97 0
|
存储 测试技术
PAT (Basic Level) Practice (中文) 1004 成绩排名 (20 分)
PAT (Basic Level) Practice (中文) 1004 成绩排名 (20 分)
64 0
|
算法
PAT (Basic Level) Practice (中文)1028. 人口普查(20分)
PAT (Basic Level) Practice (中文)1028. 人口普查(20分)
80 0
|
人工智能 测试技术
PAT (Basic Level) Practice (中文) B1008 数组元素循环右移问题 (20 分)
PAT (Basic Level) Practice (中文) B1008 数组元素循环右移问题 (20 分)
71 0
PAT (Basic Level) Practice (中文) B1008 数组元素循环右移问题 (20 分)
|
测试技术
PAT (Basic Level) Practice (中文)1012 数字分类 (20 分)+易错测试点
PAT (Basic Level) Practice (中文)1012 数字分类 (20 分)+易错测试点
98 0
PAT (Basic Level) Practice (中文)1012 数字分类 (20 分)+易错测试点
PAT (Basic Level) Practice (中文) 1010 一元多项式求导 (25 分)
PAT (Basic Level) Practice (中文) 1010 一元多项式求导 (25 分)
73 0