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的魔方。輸出將是:Magic Square Code幫助,想知道它向下移動的位置在C編程
https://i.stack.imgur.com/BYTSn.png
我想知道在代碼中,將移動4倒,因爲1已經存在。同樣的事情,7下移。原則是你每次都去1和1,如果有什麼東西你會下移並繼續前進。
旁白:「「請輸入偶數」' - >'「請輸入奇數」' –
哦,謝謝你用來捕獲 – rprog
你看了維基百科頁面[方法構建一個魔術奇數順序](https://en.wikipedia.org/wiki/Magic_square#Method_for_constructing_a_magic_square_of_odd_order)?或者[連體方法](https://en.wikipedia.org/wiki/Siamese_method)? –