解决此问题的更好方法是什么?

时间:2018-11-11 18:37:17

标签: c arrays matrix multidimensional-array

#include <stdio.h>

int main()
{
int arr[9][9];
int i = 0, x = 10;

for (int i = 0, j = 0; j <= 8; j++) {
    x++;
    arr[i][j] = x;
}
for (int j = 8, i = 1; i <= 8; i++) {
    x++;
    arr[i][j] = x;
}
for (int i = 8, j = 7; j >= 0; j--) {
    x++;
    arr[i][j] = x;
}
for (int j = 0, i = 7; i >= 1; i--) {
    x++;
    arr[i][j] = x;
}

for (int i = 1, j = 1; j <= 7; j++) {
    x++;
    arr[i][j] = x;
}
for (int j = 7, i = 2; i <= 7; i++) {
    x++;
    arr[i][j] = x;
}
for (int i = 7, j = 6; j >= 1; j--) {
    x++;
    arr[i][j] = x;
}
for (int j = 1, i = 6; i >= 2; i--) {
    x++;
    arr[i][j] = x;
}
...
arr[4][4] = x + 1;


for (int i = 0; i <= 8; i++) {
    for (int j = 0; j <= 8; j++)
        printf("%d ", arr[i][j]);
    printf("\n");
}
getch();
}

所以我有这个程序,我知道您可以循环它,但是怎么做呢?坐在一个小时的思考中,我什么都没想到。顺便说一句,任务是在图片上附加一个矩阵。有人知道这样做吗?也许使用一些复杂的循环enter image description here

4 个答案:

答案 0 :(得分:1)

这里是一种方法:

int arr[9][9] = {0};
int x = 0, i = 0, j = 0, vi = 0, vj = 1;

do {
    ++x;
    arr[i][j] = x;

    {
        int ii = i+vi;
        int jj = j+vj;
        if (ii < 0 || ii >= 9 || jj < 0 || jj >= 9 || arr[ii][jj] != 0) {
            if (vi != 0) {
                vj = -vi;
                vi = 0;
            } else {
                vi = vj;
                vj = 0;
            }
        }
    }

    i = i+vi;
    j = j+vj;
} while (arr[i][j] == 0);

Live on Coliru

这是另一种方式:

int arr[9][9] = {0};
int x = 0, i = 0, j = 0, vi = 0, vj = 1, lk = 8;

while (lk > 0) {
    for (int k = 0; k < lk; ++k) {
        ++x;
        arr[i][j] = x;
        i += vi;
        j += vj;
    }

    vi = vj;
    vj = 0;

    for (int k = 0; k < lk; ++k) {
        ++x;
        arr[i][j] = x;
        i += vi;
        j += vj;
    }

    vj = -vi;
    vi = 0;

    if (vj > 0) {
        ++i;
        ++j;
        lk -= 2;
    }
}

arr[9/2][9/2] = x+1; // Only if odd dimensions

Live on Coliru

这是另一个:

int arr[9][9] = {0};
int i = 0, lk = 8, x = 1;

while (lk > 0) {
    for (int k = 0; k < lk; ++k) {
        arr[i][i+k] = x + k;
        arr[i+k][lk+i] = x + lk + k;
        arr[lk+i][lk+i-k] = x + 2*lk + k;
        arr[lk+i-k][i] = x + 3*lk + k;
    }

    x += 4*lk;
    lk -= 2;
    ++i;
}

arr[9/2][9/2] = x; // Only if odd dimensions

Live on Coliru

答案 1 :(得分:1)

这里是for循环的“直接”选项:

#include <stdio.h>

#define N 5

int main(void) {
    int i,j,dim;
    int matrix[N][N];

    // init and print the matrix
    for (i=0; i < N; i++)
    {
        for (j=0; j< N; j++)
        {
            matrix[i][j] = i*N + j;
            printf("%2d ", matrix[i][j]);
        }
        printf("\n");
    }

    printf("\n");

    // perform spiral print
    for (dim = 0; dim < (N+1)/2; dim++)
    {
        // set initial i and go till the "last column"
        i = dim;
        for (j = dim; j < N - dim; j++)
        {
            printf("%2d ", matrix[i][j]);
        }
        printf("\n");

        // bring back i and j to the proper coordinate
        // and move down to the "last row"
        j--;i++;
        for (; i < N - dim; i++)
        {
            printf("%2d ", matrix[i][j]);
        }
        printf("\n");

        // bring back i and j to the proper coordinate
        // and move back to the "first column"
        i--;j--;
        for (; j >= dim; j--)
        {
            printf("%2d ", matrix[i][j]);
        }
        printf("\n");

        // bring back i and j to the proper coordinate
        // and move up to the "first row"
        j++;i--;
        for (; i > dim; i--)
        {
            printf("%2d ", matrix[i][j]);
        }
        printf("\n");
    }

    return 0;
}

here的输出是

 0  1  2  3  4 
 5  6  7  8  9 
10 11 12 13 14 
15 16 17 18 19 
20 21 22 23 24 

 0  1  2  3  4 
 9 14 19 24 
23 22 21 20 
15 10  5 
 6  7  8 
13 18 
17 16 
11 
12 

================================================ =========================

好像我误解了这个问题,但是从顺时针“打印”到顺时针“设置”的步骤确实很小。设置流程如下:

#include <stdio.h>

#define N 5

int main(void) {
    int i,j,dim, val = 1;
    int matrix[N][N];

    // perform spiral print
    for (dim = 0; dim < (N+1)/2; dim++)
    {
        // set initial i and go till the "last column"
        i = dim;
        for (j = dim; j < N - dim; j++)
        {
            matrix[i][j] = val++;
        }

        // bring back i and j to the proper coordinate
        // and move down to the "last row"
        j--;i++;
        for (; i < N - dim; i++)
        {
            matrix[i][j] = val++;
        }

        // bring back i and j to the proper coordinate
        // and move back to the "first column"
        i--;j--;
        for (; j >= dim; j--)
        {
            matrix[i][j] = val++;
        }

        // bring back i and j to the proper coordinate
        // and move up to the "first row"
        j++;i--;
        for (; i > dim; i--)
        {
            matrix[i][j] = val++;
        }
    }

    // print the matrix
    for (i=0; i < N; i++)
    {
        for (j=0; j< N; j++)
        {
            printf("%2d ", matrix[i][j]);
        }
        printf("\n");
    }

    return 0;
}

here所示的输出是

 1  2  3  4  5 
16 17 18 19  6 
15 24 25 20  7 
14 23 22 21  8 
13 12 11 10  9 

答案 2 :(得分:0)

我的解决方案。使用“对象” struct strangeite_s(来自陌生的ite-rator)。它可以轻松地在不同的数组上重用,甚至可以重新缩放以支持n维数组。
strangeite使用_init函数以2d数组的指定尺寸大小初始化。每次评估_loop时,xy的位置都会更新,并检查(并返回)循环结束的条件。 _inc函数应在迭代器的每个增量上调用。

#include <stdio.h>
#include <limits.h>
#include <stddef.h>
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>

struct strangeite_s {
    size_t steplen[2];
    size_t idx[2];
    size_t cursteplen;
    int direction;
};

void strangeite_init(struct strangeite_s *t, size_t max_x, size_t max_y)
{
    assert(t != NULL);
    t->steplen[0] = max_y;
    t->steplen[1] = max_x;
    memset(t->idx, 0, sizeof(t->idx));
    t->direction = 0;
    t->cursteplen = t->steplen[0];
}

bool strangeite_loop(const struct strangeite_s *t, size_t *x, size_t *y)
{
    if (x) *x = t->idx[0];
    if (y) *y = t->idx[1];
    for (size_t i = 0; i < sizeof(t->steplen)/sizeof(t->steplen[0]); ++i) {
        if (t->steplen[i] == 0) {
            return false;
        }
    }
    return true;
}

void strangeite_inc(struct strangeite_s *t)
{
    if (t->cursteplen != 1) {
        --t->cursteplen;
    } else {
        t->direction = ++t->direction % (2 * 2);
        t->cursteplen = --t->steplen[t->direction % 2];
    }
    const size_t idx_to_change = t->direction % 2;
    t->idx[idx_to_change] = t->idx[idx_to_change] + ( t->direction < 2 ? +1 : -1 );
}

int main()
{
    int var[5][5];

    struct strangeite_s  i;
    strangeite_init(&i, 5, 5);
    int idx = 0;
    for (size_t x, y; strangeite_loop(&i, &x, &y); strangeite_inc(&i)) {
        var[y][x] = ++idx; 
    }

    for (size_t i = 0; i < 5; ++i) {
        for (size_t j = 0; j < 5; ++j) {
            printf("%d ", var[i][j]);
        }
        printf("\n");
    }

    return 0;
}

产生以下输出:

1 2 3 4 5                                                                                                                                                                                                                                                   
16 17 18 19 6                                                                                                                                                                                                                                               
15 24 25 20 7                                                                                                                                                                                                                                               
14 23 22 21 8                                                                                                                                                                                                                                               
13 12 11 10 9     

onlinegdb的实时版本。

答案 3 :(得分:0)

仅在给定{{1}中的数组大小A_{i,j}N i的情况下,可以确定地计算数组j中的任何一项作为函数}},在线,没有任何状态,注意到半径是几何级数。空间需求为O(1),因为实际上并不需要数组来存储值。我们可以顺序扫描阵列。但是,就像光线跟踪一样,它可能更慢(直到一个常数,它仍然是O(1)。)

O(N^2)