//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "ms.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
int a, x, y, c,i,j; //x는 행번지, y는 열번지, c는 입력값
if (StrToInt(Number->Text)%2!=0)
{
Form1->StringGrid1->RowCount = StrToInt(Number->Text);
Form1->StringGrid1->ColCount = StrToInt(Number->Text);
for(i=0;i<StrToInt(Number->Text);i++)
{
for(j=0;j<StrToInt(Number->Text);j++)
{
StringGrid1->Cells[i][j] = "";
}
}
c=StrToInt(Number->Text);
x=0;
y=c/2; //첫번째 열번지의 수
for(a=1; a<=(c*c); a++)
{
StringGrid1->Cells[y][x]=a;
x--; //행번지 증가
y++; //열번지 증가
if((x < 0) & (y >= c)) //행과 열 모두 벗어났을때
{
x=x+2;
y=y-1;
}
else if(x < 0) //행을 벗어났을때
x=c-1;
else if(y >= c) //열을 벗어났을때
y=0;
else if(StringGrid1->Cells[y][x] != "") //이미 다른숫자가 채워져있을때
{
x=x+2;
y=y-1;
}
}
}
else
{
ShowMessage ("짝수를 입력하셨네요!");
}
}
//---------------------------------------------------------------------------
c소스는 볼랜드 c++6 enterprise으로 작성한거구 c를 약간사용했습니다.
홀수는 책보고 어떻게 해봤지만 짝수나 홀수,짝수다가능한 마방진은 못짜겠습니다.
좀 고수님께서 지도좀 해주십시요.
22일 13:00까지 제출해야하는데 정말 잘 안됩니다...
|