Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

마방진/Leonardong

From ZeroWiki
Revision as of 05:29, 7 February 2021 by imported>Unknown
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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


첫번째 방법

#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;
}

데블스캠프2003/첫째날, MagicSquare