//마방진 만들기.
~cpp 
#include <iostream>
using namespace std;

int main()
{
	int array[11][11] = {{0,},};
	cout << "마방진 프로그램을 시작하겠습니다.\n";
    	cout << " n x n 마방진 입니다.\n";
	cout << " 수를 입력하십시오.(1 ~ 9중 홀수만!)";
	int size;
	int a, b;
	int number = 1;
	int i;
	int j;
	cin >> size;
	a=1;
	b=size/2+1;
	array[a][b] = number; // 처음수 초기화.
	for (i=0;i<11;i++){
			array[0][i] = 82;
			array[i][0] = 84;
		}
		array[0][0] = 83;

	while (number < size*size) 
	{		
		number++;
		a--;
		b--;
		if(array[a][b] == 82)
		{ 
			if(array[size][b]!=0)
			{
				a=a+2;
				b=b+1;
			}
			else
				a=size;
			array[a][b] = number;
		}
			
		else if(array[a][b] == 83)
		{ 
			if(array[size][size]!=0)
			{
				a=a+2;
				b=b+1;
			}
			else
			{
				a=size;
				b=size;
			}
			array[a][b] = number;
		}
		else if (array[a][b] == 84){ 
			if(array[a][size]!=0){
				a=a+2;
				b=b+1;
			}
			else
				b=size;
			array[a][b] = number;
		}
		else if (array[a][b] == 0)
			array[a][b] = number;
		else{ 
			a=a+2;
			b=b+1;
			array[a][b] = number;
		}
	}       	
	for (i=1;i<size+1;i++){
		for(j=1;j<size+1;j++)
			cout<<array[i][j]<<"\t";
		cout<<"\n";}	
	return 0;
}	

MagicSquare
Retrieved from http://wiki.zeropage.org/wiki.php/MagicSquare/영록
last modified 2021-02-07 05:23:43