마방진,.
~cpp
#include <iostream>
using namespace std;
void main()
{
int array[19][19];
int size,row,col;
int oRow,oCol;
do
{
cout << "마방진 크기 : ";
cin >> size;
}while(size % 2 != 1);
for(int setx = 0; setx < size; setx++)
{
for(int sety =0; sety < size; sety++)
{
array[setx][sety] = 0;
}
}
row = 0;
col = size / 2 ;
array[row][col] = 1;
for(int cnt = 2; cnt <= (size * size); cnt++)
{
oRow = row;
oCol = col;
if( (row == 0) && (col == size-1) )
{
col = 0;
row = size-1;
}
else if (row == 0)
{
row = size-1;
col += 1;
}
else if (col == size-1)
{
col = 0;
row -= 1;
}
else
{
col += 1;
row -= 1;
}
if(array[row][col] != 0)
{
if(row == size-1)
{
row = oRow + 1;
col = oCol;
}
else
{
row = oRow + 1;
col = oCol;
}
}
array[row][col] = cnt;
}
for(int x = 0; x < size; x++)
{
for(int y =0; y < size; y++)
{
cout << array[x][y] << "\t";
}
cout << endl;
}
}