0
# include <stdio.h>
# include <stdlib.h>
int main(void)
{  
int s;
int row;
int column;
int k;
int array[99][99] ;

printf("Enter the dimension of the square : ") ;
scanf("%d", &s) ;

if (s % 2 == 0)
{
printf("Please enter an even number") ;
goto last;
}


column = (s + 1) / 2 ;
row = 1 ;

int sqr1 = s*s;

for(k = 1 ; k <= sqr1 ; k++)
{
array[row][column] = k ;
if(k % s == 0)
{
    row = (row + 1);
    goto loop ;
}
if(row == 1)
    row = s ;
else
         row = row - 1 ;
if(column == s)
    column = 1;
else
    column = column + 1 ;
    loop : ;
}
for (row = 1 ; row <= s ; row++)
{
for (column = 1 ; column <= s ; column++)
    {
    printf("%d\t", array[row][column]) ;
}
printf("\n\n") ;
}
last : ;
return 0;
}

コードがどこに数字を置いているか誰か教えてもらえないかと思っていました。3x3 の魔方陣が必要だとします。出力は次のようになります。

https://i.stack.imgur.com/BYTSn.png

1 が既にあるので、コードのどこで 4 を下に移動するのだろうと思っていました。7が下に移動しても同じです。原則として、毎回上に 1 つ、右に 1 つ移動し、そこに何かがある場合は下に移動して続行します。

4

1 に答える 1