PAT B brush title Road 1050 coil matrix (25 minutes)

1050 Matrix coil (25 minutes)

This problem requires a given positive integer N by a non-ascending order, fill in the "spiral matrix." The so-called "spiral matrix" refers to the upper left corner of the first one starting from the grid, filling clockwise helical direction. The required matrix size of m rows and n columns, satisfy the condition: m × n is equal to N; m≥n; m-n and the minimum value of all possible values.
Input format:
input line 1 is given in a positive integer N, the second row to be filled given positive integer number N. All numbers less than 10 ^ 4, adjacent numbers separated by a space.
Output format:
output coil matrix. Each row of n numbers, a total of m row. Neighbors are separated by a space, end of the line may not have the extra space.
Sample input:
12 is
3,776,209,876,425,395 60 81 58 93
Output Sample:
98 95 93
42 is 81 37 [
53 is 20 is 76
58 60 76

analysis:

The value of m rows and n columns is calculated first, starting from the n root of the integer part of the number N, has been pushed forward to 1, the first to find a satisfy N% n == 0, the value of m is equal to N / n;
when filling the filling by layers, a font package opening matrix is a layer, stratum level spiral matrix calculation, if the value of m is an even number, the number of layers is m / 2 if m is an odd number, the number of layers of m / 2 + 1, the level = m / 2 + m% 2; because it is starting from the upper left corner of a lattice, the filling clockwise helical direction, so that the outer control loop for i from 0 to Level layers, an inner layer for Left to right upper cycle, the upper right to the lower right, lower left to lower right, lower left order of layers to fill the upper left, note also the inner control loop for t <= N - 1, because if all of the helical elements of the matrix already they are filled with finished, you can not refillable

#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;
}

发布了73 篇原创文章 · 获赞 0 · 访问量 539

Guess you like

Origin blog.csdn.net/derbi123123/article/details/103791221