U E D R , A S I H C RSS

마방진/임민수 (rev. 1.1)

마방진/임민수

 
#include <iostream>
using namespace std;
int const arsize = 11;
void main()
{
	int num, garo=0, sero=0, cnt=1;
	cout << " 원하는 마방진의 크기를 입력하세요 (11 이하의 홀수만) : "; 
	cin >> num;
	int square[arsize][arsize]={0,};
	sero = num/2;
	square[0][sero]=1;
	while ( cnt < num*num )
	{
		garo--;
		sero++;
		if (garo < 0)
			garo = num-1;
		if (sero > num-1)
			sero = 0;
		if (square[garo][sero] !=0)
		{ garo+=2, sero-=1;}
		if (garo > num-1)
			garo = 1;
		if (sero < 0)
			sero = num-1;
		square[garo][sero]=++cnt;
	}
	
	for (int i = 0 ; i <num; i++)
	{
		for ( int j = 0 ; j < num ; j++)
		{
			cout << square[i][j] << "\t";
		}
		cout << endl;
	}
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:24
Processing time 0.0189 sec