1. 程式人生 > 其它 >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 104. 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

生詞

英文 解釋
Spiral Matrix 旋轉矩陣
clockwise 順時針的
for 為了
permutation 排列

題目大意:

將給定的N個正整數按非遞增的順序,填入“螺旋矩陣”所謂“螺旋矩陣”,是指從左上角第1個格子開始,按順時針螺旋方向填充要求矩陣的規模為m行n列,滿足條件:m*n等於N;m>=n;且m-n取所有可能值中的最小值~

分析:

先計算行數m和列數n的值,n從根號N的整數部分開始,往前推一直到1,找到第一個滿足N % n== 0的,m的值等於N/n~將N個給定的值輸入陣列a,並將a陣列中的值按非遞增排序,接著建立m行n列的陣列b,填充時按層數填充,一個包裹矩陣的口字型為一層,計算螺旋矩陣的層數level,如果m的值為偶數,層數為m/2,如果m為奇數,層數為m/2+1,所以level = m / 2 + m % 2;因為是從左上角第1個格子開始,按順時針螺旋方向填充,所以外層for迴圈控制層數i從0到level,內層for迴圈按左上到右上、右上到右下、右下到左下、左下到左上的順序一層層填充,注意內層for迴圈中還要控制t <= N – 1,因為如果螺旋矩陣中所有的元素已經都填充完畢,就不能再重複填充~填充完畢後,輸出整個矩陣~

原文連結:https://blog.csdn.net/liuchuo/article/details/52123228

柳神題解

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int cmp(int a, int b) {return a > b;}
int main() {
    int N, m, n, t = 0;
    scanf("%d", &N);
    for (n = sqrt((double)N); n >= 1; n--) {
        if (N % n == 0) {
            m = N / n;
            break;
        }
    }
    vector<int> a(N);
    for (int i = 0; i < N; i++)
        scanf("%d", &a[i]);
    sort(a.begin(), a.end(), cmp);
    vector<vector<int> > b(m, vector<int>(n));
    int level = m / 2 + m % 2;
    for (int i = 0; i < level; i++) {
        for (int j = i; j <= n - 1 - i && t <= N - 1; j++)
                b[i][j] = a[t++];
        for (int j = i + 1; j <= m - 2 - i && t <= N - 1; j++)
                b[j][n - 1 - i] = a[t++];
        for (int j = n - i - 1; j >= i && t <= N - 1; j--)
                b[m - 1 - i][j] = a[t++];
        for (int j = m - 2 - i; j >= i + 1 && t <= N - 1; j--)
                b[j][i] = a[t++];
    }
    for (int i = 0; i < m; i++) {
        for (int j = 0 ; j < n; j++) {
            printf("%d", b[i][j]);
            if (j != n - 1) printf(" ");
        }
        printf("\n");
    }
    return 0;
}

不知道哪錯了的抄都抄錯了的爛程式碼

#include <bits/stdc++.h>

using namespace std;
bool cmp(int a,int b) { return a>b;}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif
    int s,m,n,t=0;
    cin>>s;
    for(n=sqrt((double)s);n>0;n--){
        if(s%n==0) m=s/n;
        break;
    }
    vector<int> v(s);
    for(int i=0;i<s;i++){
        scanf("%d", &v[i]);
    }
    sort(v.begin(),v.end(),cmp);
    vector<vector<int> > b(m,vector<int>(n));
    int level=m/2+m%2;
    for(int i=0;i<level;i++){
        for(int j=i;j<=n-1-i&&t<=s-1;j++)
            b[i][j]=v[t++];
        for(int j=i+1;j<=m-2-i&&t<=s-1;j++)
            b[j][n-1-i]=v[t++];
        for(int j=n-i-1;j>=i&&t<=s-1;j--)
            b[m-1-i][j]=v[t++];
        for(int j=m-2-i;j>=i+1&&t<=s-1;j--)
            b[j][i]=v[t++];
    }
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            printf("%d", b[i][j]);
            if(j!=n-1) printf(" ");
        }
        printf("\n");
    }
    return 0;
}

本文來自部落格園,作者:勇往直前的力量,轉載請註明原文連結:https://www.cnblogs.com/moonlight1999/p/15760455.html