U E D R , A S I H C RSS

Magic Square/동기

~cpp 
#include <iostream>
using namespace std;

int number[9][9]={{0,},};
void main()
{
	int k;
    int i=0;
	cout<<"마방진 작성 프로그램입니다. 홀수를 입력하세요 >> ";
	while(i==0)
	{
		cin >> k;
		i = k%2;
	}
	int MAX = k-1;
	int x = k/2; 
	int y = 0;
	int col=1;
	int final = k*k;
	int count= 1;
	number[y][x]=count;
	
	for(count=2;count<=final;count++)
	{
		int newy=y;
		int newx=x;
		
		y--;
		x++;
		if (y<0)
			y=MAX;
		if (x>MAX)
			x=0;
		if (number[y][x]==0)
				number[y][x]=count;
		else
		{
			newy++;
			if (newy>MAX)
				newy=0;
			number[newy][newx]=count;
			y=newy;
			x=newx;		
		}
	}
	for (int p=0;p<=MAX;p++)
	{
		for (int l=0;l<=MAX;l++)
		{
			cout << number[p][l]<<"\t";
		}
		cout<<endl;
	}

}
		

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:43
Processing time 0.0120 sec