U E D R , A S I H C RSS

whiteblue/Magic Square

No older revisions available

No older revisions available



~cpp 
#include <iostream>
using namespace std;
int arr[20][20] = {{0}};
int size;

void magicsquare();
void show ();

int main()
{
	cout << "사이즈를 입력하십시오(홀수만) : ";
	cin >> size;
	magicsquare();
	show ();
	return 0;
}



void magicsquare()
{
	int x = size/2;
	int i = x, j = 0, count = 1;
	while(count <= size*size)
	{
		if ( arr[i][j] == 0)
		{
			arr[i][j] = count;
			count++;
		}
		else
		{
			i ++;
			j ++;
			if ( i == size )
				i = 0;
			if ( j == size )
				j = 0;
			j ++;
			if ( j == size )
				j = 0;
			continue;
		}
		i--;
		j--;
		if (i == -1 )
			i = size-1;
		if (j == -1 )
			j = size-1;
	}
}

void show () 
{
	for (int i=0; i<size; i++) 
	{
		for (int j=0; j<size; j++)
		{
			cout << arr[j][i] << "\t";
		}
		cout << endl;
	}
	cout << endl;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:31:46
Processing time 0.0281 sec