PAT A1105 Spiral Matrix [硬核模拟]

题目描述

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

分析

  • 方法1:就是按照题意模拟,我自己想到一个切换方向的方法,但是必须初始化的时候要注意!
  • 方法2:while里面套while。内层while有4个,组成了一个整体,即走了一圈,外层while表示不断走圈,直到走完
  • 一直最后一个样例错!!!!!一般都是大样例!!!检查下开的数组,发现没有指定范围!!!
  • 此时!!!用vector!!!
vector<int> a(N); //一维数组
vector<vector<int> > b(m, vector<int>(n)); //二维数组!!!有m行,每行是一个n列的一维数组!
  • 二维数组初始化
memset(a, -1,sizeof(a)); //一样的

代码1

#include<bits/stdc++.h>
using namespace std;

const int maxn = 1000;
int a[maxn][maxn];
bool v[maxn][maxn];
const int maxm = 1e4+10;
int b[maxm];
int t,m,n;

int main(){
    cin>>t;
    for(int i=1; i<=(int)sqrt(t); i++){
        if(t % i == 0) n = i;
    }
    m = t / n;
    for(int i=0;i<t;i++){
        scanf("%d",&b[i]);
    }
    sort(b,b+t);
    reverse(b,b+t);
    int i = 0, j = -1, nxt = 0, cnt = 0;
    while(cnt < t){
        if(nxt == 0){
            j++;
            if(j>=n || v[i][j]){
                nxt = (nxt + 1) % 4;
                j--;
            }else{
                v[i][j] = 1;
                a[i][j] = b[cnt++];
            }
        }else if(nxt == 1){
            i++;
            if(i>=m || v[i][j]){
                nxt = (nxt + 1) % 4;
                i--;
            }else{
                v[i][j] = 1;
                a[i][j] = b[cnt++];
            }
        }else if(nxt == 2){
            j--;
            if(j<0 || v[i][j]){
                nxt = (nxt+1)%4;
                j++;
            }else{
                v[i][j] = 1;
                a[i][j] = b[cnt++];
            }
        }else{
            i--;
            if(i<0 || v[i][j]){
                nxt = (nxt+1)%4;
                i++;
            }else{
                v[i][j] = 1;
                a[i][j] = b[cnt++];
            }
        }
    }
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            if(j!=0) printf(" ");
            printf("%d",a[i][j]);
        }
        printf("\n");
    }

}

代码2

#include<bits/stdc++.h>
using namespace std;

const int maxn = 1e4+10;
int a[500][500];
int b[maxn];
int t,m,n;
bool cmp(int x, int y){
    return x>y;
}
int main(){
    scanf("%d",&t);
    for(n=sqrt((double)t); n>=1; n--){
        if(t % n == 0){
            m = t / n;
            break;
        }
    }
    for(int i=0;i<t;i++){
        scanf("%d",&b[i]);
    }
    sort(b,b+t, cmp);
    int i = 0, j = 0, cnt = 0;
    memset(a, -1, sizeof(a));
    while(cnt != t){
        while(i>=0&&j>=0&&j<n&&i<m&&a[i][j]==-1&&cnt<t) a[i][j++]=b[cnt++];
        i++, j--;
        while(i>=0&&j>=0&&j<n&&i<m&&a[i][j]==-1&&cnt<t) a[i++][j]=b[cnt++];
        i--, j--;
        while(i>=0&&j>=0&&j<n&&i<m&&a[i][j]==-1&&cnt<t) a[i][j--]=b[cnt++];
        i--, j++;
        while(i>=0&&j>=0&&j<n&&i<m&&a[i][j]==-1&&cnt<t) a[i--][j]=b[cnt++];
        i++, j++;
    }
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            if(j!=0) printf(" ");
            printf("%d",a[i][j]);
        }
        printf("\n");
    }

}

原文地址:https://www.cnblogs.com/doragd/p/11385808.html

时间: 2024-07-30 15:29:17

PAT A1105 Spiral Matrix [硬核模拟]的相关文章

1105. Spiral Matrix (25)【模拟】——PAT (Advanced Level) Practise

题目信息 1105. Spiral Matrix (25) 时间限制150 ms 内存限制65536 kB 代码长度限制16000 B 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 co

PAT 1105 Spiral Matrix

1105 Spiral Matrix(25 分) 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

PAT A1153 Decode Registration Card of PAT [硬核模拟]

题目描述 链接 给出一组学生的准考证号和成绩,准考证号包含了等级(乙甲顶),考场号,日期,和个人编号信息,并有三种查询方式 查询一:给出考试等级,找出该等级的考生,按照成绩降序,准考证升序排序 查询二:给出考场号,统计该考场的考生数量和总得分 查询三:给出考试日期,查询改日期下所有考场的考试人数,按照人数降序,考场号升序排序 分析 查询一和查询二都是可以直接遍历做到的!!!直接遍历,不用存中间状态!!!否则用map超时 查询三用unordered_map 注意!!不需要提前把所有都提取出来,变成

PAT_A1105#Spiral Matrix

Source: PAT A1105 Spiral Matrix (25 分) Description: 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

PAT 甲级 1105 Spiral Matrix (25分)(螺旋矩阵,简单模拟)

1105 Spiral Matrix (25分) 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

PAT 甲级 1105 Spiral Matrix

https://pintia.cn/problem-sets/994805342720868352/problems/994805363117768704 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 upp

[LeetCode]59.Spiral Matrix II

[题目] Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] [分析] 模拟 [代码] /**-------------------------

一套硬核安防视频云平台的炼成

导读 三大特点.五大重点,一套硬核安防视频云平台是这样炼成的 很多人会困惑,云计算究竟是什么? 它是可以被看作为商品的一种计算能力,可以在线上被无限制流通,就如水.电.煤气一样,可以被人们方便地取用,且价格相较低廉. 十多年前,无论是中国.美国,亚洲.欧洲,无论是大企业还是小公司,建设企业 IT 架构,为公司的业务和战略做信息支撑的思路都如出一辙——购买服务器,只是多或少的问题. 今天来看,这类 IT 基础设施的布局是一项非常不必要的高额支出,因为他们可以通过租用“公有云”的云计算服务来获取性价

Spiral Matrix(LintCode)

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 难得的一次AC! 虽然感觉题