U E D R , A S I H C RSS

마방진/민강근

~cpp 

하다만거

#include<iostream>
#include<vector>
using namespace std;

int main()
{
	int m;
	cout<<"몇 행, 몇 열 마방진을 만드실껀가여? : ";
	cin>>m;
	vector <vector <int> > ma; 
 
        ma.resize(m); 
 
        for (int i = 0 ; i < m ; i++) 
        { 
                ma[i].resize(m); 
        } 

	int h=0, y, input=1;
		
	for(h = 0 ; h < m ; h++ )
	{
		for(y = 0 ; y < m ; y++ )
		ma[h][y]=0;
	}

	for(h = 0 , y = m/2 ; input == m*m ; h--, y++, input++)
	{
		if(h<0 && y>=0 && y <=m)
		{	
			h=m; 
			ma[h][y]=input;
		}
		if(h>0 && y>m )
		{
			y=0;
			ma[h][y]=input;
		}
		if(h<0 && y>m )
		{
			y=0, h=m;
			ma[h][y]=input;
		}
		if(h>0 && y>=0 && y <=m)
		{
			ma[h][y]=input;
		}
	}
	for(h = 0 ; h < m ; h++ )
	{
		for(y = 0 ; y < m ; y++ )
			cout<<ma[h][y]<<"\t";
		cout<<endl;
	}
	return 0;
}












/*    for(h = -1 , y = 1 , input =2 ; input <= (m*m) ;  h-- , y ++, input ++)
	{	
		if(h<0)
			h=h+m;
		if(ma[h%m][(first+y)%m] == 0)
		{
			ma[h%m][(first+y)%m] = input;
		}
		else
		{
			ma[h%m+1][(first+y)%m] = input;
		}
	}
*/

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2009-05-27 07:09:19
Processing time 0.0082 sec