Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何编写一个代码,在c中使用for循环(无数组)打印特定的数字螺旋图案_C_For Loop_While Loop_Spiral - Fatal编程技术网

如何编写一个代码,在c中使用for循环(无数组)打印特定的数字螺旋图案

如何编写一个代码,在c中使用for循环(无数组)打印特定的数字螺旋图案,c,for-loop,while-loop,spiral,C,For Loop,While Loop,Spiral,我想编写一个代码,从用户那里获取整数n,并以螺旋模式打印从1到n*n的数字,而不使用数组。 你对如何编写这段代码有什么建议吗 编辑: 以下是使用数组的代码: #include <stdio.h> int main(){ /*declaration of the variables*/ int i, j, ascendingNumbers; int leftAndTop, rightAndBottom; int size; scanf("%d", &size); int m

我想编写一个代码,从用户那里获取整数n,并以螺旋模式打印从1到n*n的数字,而不使用数组。

你对如何编写这段代码有什么建议吗

编辑: 以下是使用数组的代码:

#include <stdio.h>

int main(){
/*declaration of the variables*/
int i, j, ascendingNumbers;
int leftAndTop, rightAndBottom;
int size;
scanf("%d", &size);
int matrix[size][size];

leftAndTop = 0;
rightAndBottom = size - 1;
ascendingNumbers = 1;

/*filling the array*/
for(i = 1; i <= size/2; i++, leftAndTop++, rightAndBottom--){
    /*left to right*/
    for(j = leftAndTop; j <= rightAndBottom; j++, ascendingNumbers++){
        matrix[leftAndTop][j] = ascendingNumbers;
    }

    /*top to bottom*/
    for(j = leftAndTop + 1; j <= rightAndBottom; j++, ascendingNumbers++){
        matrix[j][rightAndBottom] = ascendingNumbers;
    }

    /*right to left*/
    for(j = rightAndBottom-1; j >= leftAndTop; j--, ascendingNumbers++){
        matrix[rightAndBottom][j] = ascendingNumbers;
    }

    /*bottom to top*/
    for(j = rightAndBottom - 1; j >= leftAndTop+1; j--, ascendingNumbers++){
        matrix[j][leftAndTop] = ascendingNumbers;
    }
}

/*fill the center for odd size*/
if(size % 2){
    matrix[leftAndTop][j + 1] = ascendingNumbers;
}

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

return 0;
}
#包括
int main(){
/*变量的声明*/
整数i,j,递增数;
int leftAndTop、right和bottom;
整数大小;
scanf(“%d”,大小(&S);
整数矩阵[大小][大小];
leftAndTop=0;
rightAndBottom=大小-1;
上升数=1;
/*填充数组*/

对于(i=1;i一种解决方案是使用已经可以用螺旋模式填充数组的代码,并将其放入嵌套循环中,使用该代码查找当前打印位置的编号。当然有更有效的解决方案,但这允许您将适用于数组的解决方案转换为不需要数组的解决方案

这里的第一个程序使用数组来打印数字的螺旋模式。第二个程序是第一个程序的修改版本,当打印位置与螺旋循环中的位置匹配时打印数字,而不是将其存储在数组中。我将让您看看是否可以修改现有代码来完成此操作

使用二维阵列:

/* A program that prints a spiral using a 2d array */

#include <stdio.h>

int main(int argc, char *argv[])
{
    int size;
    if (argc < 2 || (sscanf(argv[1], "%d", &size) != 1) || size < 1) {
        fprintf(stderr, "Usage: spiral N  [N > 0]\n");
        return 1;
    }

    int arr[size][size];
    int num_elems = size * size;


    enum Dir { RIGHT, DOWN, LEFT, UP };
    int num_directions = 4;

    int side_len = size;
    int row = 0;       // arr row index
    int col = 0;       // arr column index
    int pos = 0;       // position in a side

    // travel around the spiral to fill the array
    enum Dir direction = RIGHT;
    for (int i = 0; i < num_elems; i++) {
        arr[row][col] = i + 1;
        ++pos;

        if (pos == side_len) {                             // change direction
            direction = (direction + 1) % num_directions;
            pos = 0;

            // having changed direction, shorten side_len in two cases
            if (direction == DOWN || direction == UP) {
                --side_len;
            }
        }

        switch (direction) {
        case RIGHT:
            ++col;
            break;
        case DOWN:
            ++row;
            break;
        case LEFT:
            --col;
            break;
        case UP:
            --row;
            break;
        default:
            fprintf(stderr, "Unexpected value in switch statement\n");
            return 1;
        }
    }

    for (row = 0; row < size; row++) {
        for (col = 0; col < size; col++) {
            printf("%4d", arr[row][col]);
        }
        putchar('\n');
    }
    putchar('\n');

    return 0;
}

不,这个问题应该通过for和while循环来解决。我使用数组编写了代码,但是我需要在没有数组的情况下编写代码,我无法这样做。我编辑了文章并添加了代码
/* A program that prints a spiral using a 2d array */

#include <stdio.h>

int main(int argc, char *argv[])
{
    int size;
    if (argc < 2 || (sscanf(argv[1], "%d", &size) != 1) || size < 1) {
        fprintf(stderr, "Usage: spiral N  [N > 0]\n");
        return 1;
    }

    int arr[size][size];
    int num_elems = size * size;


    enum Dir { RIGHT, DOWN, LEFT, UP };
    int num_directions = 4;

    int side_len = size;
    int row = 0;       // arr row index
    int col = 0;       // arr column index
    int pos = 0;       // position in a side

    // travel around the spiral to fill the array
    enum Dir direction = RIGHT;
    for (int i = 0; i < num_elems; i++) {
        arr[row][col] = i + 1;
        ++pos;

        if (pos == side_len) {                             // change direction
            direction = (direction + 1) % num_directions;
            pos = 0;

            // having changed direction, shorten side_len in two cases
            if (direction == DOWN || direction == UP) {
                --side_len;
            }
        }

        switch (direction) {
        case RIGHT:
            ++col;
            break;
        case DOWN:
            ++row;
            break;
        case LEFT:
            --col;
            break;
        case UP:
            --row;
            break;
        default:
            fprintf(stderr, "Unexpected value in switch statement\n");
            return 1;
        }
    }

    for (row = 0; row < size; row++) {
        for (col = 0; col < size; col++) {
            printf("%4d", arr[row][col]);
        }
        putchar('\n');
    }
    putchar('\n');

    return 0;
}
/* A program that prints a spiral using loops but no arrays */

#include <stdio.h>

int main(int argc, char *argv[])
{
    int size;
    if (argc < 2 || (sscanf(argv[1], "%d", &size) != 1) || size < 0) {
        fprintf(stderr, "Usage: spiral N  [N >= 0]\n");
        return 1;
    }

    int num_elems = size * size;

    enum Dir { RIGHT, DOWN, LEFT, UP };
    int num_directions = 4;

    // loop printing positions: print a row at a time
    for (int y = 0; y < size; y++) {
        for (int x = 0; x < size; x++) {
            int side_len = size;          // length of current side
            int row = 0;                  // arr row index
            int col = 0;                  // arr column index
            int pos = 0;                  // position in a side

            // travel around spiral until printing number is reached
            enum Dir direction = RIGHT;
            for (int i = 0; i < num_elems; i++) {
                if (row == y && col == x) {  // print and escape loop
                    printf("%4d", i + 1);
                    break;
                }
                ++pos;

                if (pos == side_len) {                     // change direction
                    direction = (direction + 1) % num_directions;
                    pos = 0;

                    // having changed direction, shorten side_len in two cases
                    if (direction == DOWN || direction == UP) {
                        --side_len;
                    }
                }

                switch (direction) {
                case RIGHT:
                    ++col;
                    break;
                case DOWN:
                    ++row;
                    break;
                case LEFT:
                    --col;
                    break;
                case UP:
                    --row;
                    break;
                default:
                    fprintf(stderr, "Unexpected value in switch statement\n");
                    return 1;
                }
            }
        }
        // newline after row
        putchar('\n');
    }
    // newline after printing all numbers
    putchar('\n');

    return 0;
}