1105 Spiral Matrix (25point(s)) - C语言 PAT 甲级

1105 Spiral Matrix (25point(s))

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

题目大意:

1050 螺旋矩阵 (25point(s))

设计思路:

1050 螺旋矩阵(C语言)

  • 总体设计方向是一层一层填充 m x n 的矩阵
  1. 确定 m 和 n 的值,n 从 sqrt(N) 趋于 0,第一个 N % n == 0 成立即找到
  2. 确定矩阵有几层,level = n / 2 + n % 2
  3. 每一层有四个方向
  • 注意:如何保存矩阵?一种方法是直接利用一维数组模拟二维,但操作相对繁琐,本文采用变长数组进行保存,较为方便。不要预设矩阵为 10000 x 10000 大小后进行操作,提交后最后一个测试点会返回各种不同的结果,个人推测是因为计算过程中要对二维数组进行大量的寻址,造成大量时间空间的浪费,如下极端情况:N 为 9973 素数时矩阵形状
    9973 0 ……
    . 0 0 0 ……
    . 0 0 0 ……
    . 0 0 0 ……
    3 0 0 0 ……
    2 0 0 0 ……
    1 0 0 0 ……
    所有 0 的位置均无实际数据,但因二维数组在内存中是“一维”的,每次存取均要经过很多“0”才能到达正确地址
编译器:C (gcc)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int comp(const void *a, const void *b)
{
    return *(int*)b - *(int*)a;
}


int main()
{
    int N, m, n, num[10000];
    int i, j, k,level;

    scanf("%d", &N);
    for(i = 0; i < N; i++)
        scanf("%d", num + i);

    qsort(num, N, sizeof(int), comp);
    for(n = sqrt((double)N); !(n > 0 && N % n == 0); n--)
        continue;
    m = N / n;

    int matrix[m][n];
    level = n / 2 + n % 2;
    for(i = 0, k = 0; i < level; i++){
        for(j = i; k <= N - 1 && j <= n - i - 1; j++)
            matrix[i][j] = num[k++];
        for(j = i + 1; k <= N - 1 && j <= m - i - 2; j++)
            matrix[j][n - i - 1] = num[k++];
        for(j = n - i - 1; k <= N - 1 && j >= i; j--)
            matrix[m - i -1][j] = num[k++];
        for(j = m - 2 - i; k <= N - 1 && j >= i + 1; j--)
            matrix[j][i] = num[k++];
    }

    for(i = 0; i < m; i++)
        for(j = 0; j < n; j++)
            printf("%d%c", matrix[i][j], j != n -1 ? ' ' : '\n');

    return 0;
}
基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip基于bert实现关系三元组抽取python源码+数据集+项目说明.zip 个人大四的毕业设计、课程设计、作业、经导师指导并认可通过的高分设计项目,评审平均分达96.5分。主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 [资源说明] 不懂运行,下载完可以私聊问,可远程教学 该资源内项目源码是个人的毕设或者课设、作业,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96.5分,放心下载使用! 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),供学习参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值