//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "RzButton"
#pragma link "RzEdit"
#pragma link "RzLabel"
#pragma link "RzSpnEdt"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Incr ()
{
int Loc = DCnt -1;
while ( 1 == 1 )
if ( ++ Sums [Loc] > 9 )
{
if ( Loc == 0 ) return;
Sums [Loc] = Loc;
Loc --;
}
else
return;
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::Calc ()
{
int i, j, S = 0;
for ( i = 0; i < DCnt; i ++ )
S += Sums [i];
if ( S != Val ) return false;
for ( i = 0; i < DCnt; i ++ )
for ( j = i + 1; j < DCnt; j ++ )
if ( Sums [i] == Sums [j] ) return false;
return true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PrintfRes()
{
AnsiString Vals = "";
for ( int i = 0; i < DCnt; i ++ )
Vals = Vals + IntToStr ( Sums [i] );
Res->Lines->Add ( Vals );
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::ChechLines()
{
int i, j, Same = 0;
for ( i = 0; i < Res->Lines->Count; i ++ )
{
for ( Same = j = 0; j < DCnt; j ++ )
if ( Res->Lines->Strings [i].Pos(IntToStr ( Sums[j] ) ) > 0 )
Same ++;
if ( Same == DCnt ) return false;
}
return true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CalcBtnClick(TObject *Sender)
{
int i, Loc;
DCnt = Digits->Text.ToIntDef(0); // Digit Count
Val = Sum->Text.ToIntDef(0); // Value
Res->Lines->Clear ();
if ( DCnt <= 1 || DCnt > 9 )
{
ShowMessage ( "Are you OK?" );
return;
}
for ( i = 0; i < DCnt; i ++ )
Sums [i] = i + 1;
Sums [0] = 1;
while ( Sums [0] < 10 )
{
if ( Calc () && ChechLines () )
PrintfRes ();
Incr ();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyPress(TObject *Sender, wchar_t &Key)
{
if ( Key == VK_RETURN ) SelectNext ( ActiveControl, true, true );
if ( Key == VK_ESCAPE ) SelectFirst ();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
if ( Key == VK_F2 )
{
CalcBtnClick ( CalcBtn );
SelectFirst ();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CloseBtnClick(TObject *Sender)
{
Close ();
}
//---------------------------------------------------------------------------