U E D R , A S I H C RSS

마방진/Leonardong

데블스 캠프 첫째날 마방진 코드

첫번째 방법
~cpp 
#include <iostream>
using namespace std;
const int Asize=19;

int main()
{
	int input;
	cout << "원하는 마방진 크기 : ";
	cin >> input;
	int i=1, count=0 ,x=input/2 ,y=0;
	int ground[Asize][Asize]={0,};
	while ( count < input*input )
	{
		if (ground[x][y]==0)
		{
			ground[x][y]=i;
			++i, ++count, ++x, --y;
		}
		else//범위를 넘어가는 경우를 조심해야지...
		{
			--x, ++y;
			if (x<0)
				x=input-1;
			if (y>input-1)
				y=0;
			++y;
			if (y>input-1)
				y=0;
			
		}
		if (x<0)
			x=input-1;
		else if(x>input-1)
			x=0;
		if (y<0)
			y=input-1;
		else if (y>input-1)
			y=0;
	
	}
	
	for (int j=0 ; j<input ; j++)
	{
		for (int k=0 ; k<input ; k++)
			cout << ground[k][j] << "\t";
		cout << endl;
	}
	return 0;
}

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:24
Processing time 0.0195 sec