2

所以我已经为这个例子苦苦挣扎了一个小时,我什至无法开始处理我应该如何做这个。

编写一个程序,对于给定的 n 和 m,形成所描述的矩阵。矩阵应该是 mxm,并且从左上角开始以“螺旋状”填充。矩阵中的第一个值是数字 n。它一直重复到矩阵的“边缘”,此时数字增加。在数字 9 变为 0 之后。0 ≤ n ≤ 9, 0 ≤ m ≤ 9

n=3, m=5 的示例

4

2 回答 2

1

前段时间我做了一个函数来在一个奇数大小的网格上显示数字 1 到 n。原则是从中心开始,然后移动;

  • x = 1
  • 右边的 x 框
  • 底部的 x 框
  • x++
  • 左边的 x 框
  • 顶部的 x 框
  • x++

使用这个简单的算法,您可以轻松地想象从问题的中心开始并减少您的值,从中心开始似乎更容易。


这是说明上述解决方案的代码,当然要针对您的问题进行调整,这只是一个线索。

#define WE 5

void    clock(int grid[WE][WE])
{
    int count;
    int i;
    int reach;
    int flag;
    int tab[2] = {WE / 2, WE / 2}; //x , y

    count = 0;
    flag = 0;
    i = 0;
    reach = 1;
    grid[tab[1]][tab[0]] = count;
    for (int j = 0; j < WE - 1 && grid[0][WE - 1] != pow(WE, 2) - 1; j++)
        for (i = 0; i < reach && grid[0][WE - 1] != pow(WE, 2) - 1; i++, reach++)
        {
            if(flag % 2 == 0)
            {
                for(int right = 0 ; right < reach ; right++, tab[0]++, count++, flag = 1)
                    grid[tab[1]][tab[0]] = count;
                if(reach < WE - 1)
                    for(int bottom = 0; bottom < reach; bottom++, count++, tab[1]++)
                        grid[tab[1]][tab[0]] = count;
            }
            else
            {
                for(int left = 0; left < reach; left++, count++, tab[0]--, flag = 0)
                    grid[tab[1]][tab[0]] = count;
                for(int top = 0; top < reach; top++, tab[1]--, count++)
                    grid[tab[1]][tab[0]] = count;
            }
        }
}
于 2020-12-13T11:23:50.100 回答
0

我终于解决了。如果有人感兴趣,我是这样做的:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//Fills the row number "row" with the number n
int fillRow(int m, int n, int arr[m][m], int row)
{
    int j;

    for(j=0;j<m;j++)
    {
        if(arr[row][j] == -1 || arr[row][j] == n-1) arr[row][j] = n;
    }

}

//Fills the column number "col" with the number n
int fillCol(int m, int n, int arr[m][m], int col)
{
    int i;

    for(i=0;i<m;i++)
    {
        if(arr[i][col] == -1 || arr[i][col] == n-1) arr[i][col] = n;
    }

}

int main()
{

    int n, m, i, j, r=1, c=1, row=-1, col=-1;

    scanf("%d %d",&n, &m);

    int arr[m][m];
    
    
    //Fill array with -1 everywhere
    for(i=0;i<m;i++)
    {
        for(j=0;j<m;j++)
        {
            arr[i][j] = -1;
        }

    }
    
    //Calculate which row/column to fill (variables row/col)
    //Fill row then column then row than column...
    for(i=0;i<2*m;i++)
    {
        if(i%2==0)
        {
            row = (r%2==0) ? m-r/2 : r/2;
            fillRow(m, n, arr, row);
            n++;
            r++;
        }
        else if(i%2==1)
        {
            col = (c%2==0) ? c/2-1 : m-c/2-1;
            fillCol(m, n, arr, col);
            n++;
            c++;
        }
    }
    
    //If an element is larger than 9, decrease it by 10
    //Prints the elements
    for(i=0;i<m;i++)
    {
        for(j=0;j<m;j++)
        {
            if(arr[i][j]>9) arr[i][j] -=10;
            printf("%d ",arr[i][j]);
        }
        printf("\n");
    }

    return 0;
}
于 2020-12-13T11:53:17.347 回答