zoukankan      html  css  js  c++  java
  • 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 matrix has mrows and n columns, where m and n satisfy the following: m×n must be equal to N; mn; and mn 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

     题目大意:输入一个数N,并且有N个数,给出一个非递增的螺旋矩阵,m是行,n是列,m*n=N,要求m>=n,并且在所有可能的取值中,m-n的值最小。

     //1.首先就需要对N进行因式分解,确定m和n。2.其次就放就可以了。

    //N的范围没给。 不太明白螺旋矩阵的下标是什么规律。需要先学习一下螺旋矩阵的算法。

    #include <iostream>
    #include <cmath>
    #include <vector>
    #include <algorithm>
    using namespace std;
    int func(int N) {
        int i = sqrt((double)N);
        while(i >= 1) {
            if(N % i == 0)//i是这样递减的,那么肯定能够保证是差值最小的了。
                return i;
            i--;
        }
        return 1;
    }
    int cmp(int a, int b) {return a > b;}
    int main() {
        int N, m, n, t = 0;
        scanf("%d", &N);
        n = func(N);//如果是12,那么n=3;
        m = N / n;
        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));//m行,这么多列。
        int level = m / 2 + m % 2;
        for (int i = 0; i < level; i++) {//i表示是第几圈。
            for (int j = i; j <= n - 1 - i && t <= N - 1; j++)//t是控制原数组不越界。
                    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;
    }

    //看了这个代码,还是稍微有点不太明白,需要复习,一下。

    坐标的起始点和控制范围,

    首先是向右的时候 :第几圈就是那个起始点,那么列的范围就是n-1-i;向下的时候,行的范围就是i+1开始,n-1-i列,这个需要多去实现一下。 

  • 相关阅读:
    桌面应用程序ClickOne打包部署
    Linux系统基础5周入门精讲(Linux发展过程)
    Linux系统基础5周入门精讲(服务器介绍)
    算法进阶--动态规划
    行为型模式
    结构型模式
    设计模式
    哈希表--树
    数据结构
    查找排序相关面试题
  • 原文地址:https://www.cnblogs.com/BlueBlueSea/p/9538403.html
Copyright © 2011-2022 走看看