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 matrix has m rows and n columns, where m and n satisfy the following: m×n must be equal to N; m≥n; and m−n is the minimum of all the possible values.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N. Then the next line contains N positive integers to be filled into the spiral matrix. All the numbers are no more than 1. The numbers in a line are separated by spaces.

Output Specification:

For each test case, output the resulting matrix in m lines, each contains n numbers. There must be exactly 1 space between two adjacent numbers, and no extra space at the end of each line.

Sample Input:

12
37 76 20 98 76 42 53 95 60 81 58 93

Sample Output:

98 95 93
42 37 81
53 20 76
58 60 76

题意:

用这个数列的数,形成一个螺旋矩阵(顺时针)从大到小

题解:

1.将数列排序
2.分别填入向右向下向左向上四个方向的数字,一次控制 列坐标增加,行坐标增加,列坐标减少,行坐标减少。

AC代码:

#include<bits/stdc++.h>
using namespace std;
int N;
int a[100005];
int n,m;
int ma[105][105];
int main(){
    memset(a,0,sizeof(a));
    cin>>N;
    for(int i=1;i<=N;i++) cin>>a[i];
    sort(a+1,a+1+N);
    for(int i=1;i<=sqrt(N);i++){
        if(N%i==0){
            m=i;
            n=N/i;
        }
    }
    //cout<<n<<" "<<m<<endl;
    int x=0,y=1;
    int d=1;
    for(int i=N;i>=1;i--){
        if(d==1){
            x++;
            if(x>m||ma[y][x]!=0){
                d=2;
                x--;
                y++;
            }
        }else if(d==2){
            y++;
            if(y>n||ma[y][x]!=0){
                d=3;
                y--;
                x--;
            }
        }else if(d==3){
            x--;
            if(x<1||ma[y][x]!=0){
                d=4;
                x++;
                y--;
            }
        }else{
            y--;
            if(y<1||ma[y][x]!=0){
                d=1;
                y++;
                x++;
            }
        }
        ma[y][x]=a[i];
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cout<<ma[i][j];
            if(j!=m) cout<<" ";
            else cout<<endl;
        }
    }
    return 0;
} 

原文地址:https://www.cnblogs.com/caiyishuai/p/12234668.html

时间: 2024-09-30 06:40:03

PAT 甲级 1105 Spiral Matrix (25分)(螺旋矩阵,简单模拟)的相关文章

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

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

每日算法之四十一: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 ] ] 针对这个问题采用最直观的方式即可,即螺旋插入,这里有两个地方需要注意,一个是插入边界的界定,

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 matrix has m rows and n c

PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)

1016 Phone Bills (25 分)   A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connec

PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)

1024 Palindromic Number (25 分) A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers. Non-palindromic n

PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

1028 List Sorting (25 分)   Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤) and C, wh

PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)

1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if

PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)

1063 Set Similarity (25 分)   Given two sets of integers, the similarity of the sets is defined to be /, where N?c?? is the number of distinct common numbers shared by the two sets, and N?t?? is the total number of distinct numbers in the two sets. Yo