//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void TForm1::main() { char *stime[30],*etime[30]; __int64 tick1,tick2; tick1 = GetTickCount(); Form1->Cursor = crHourGlass; fid = fopen("log.txt","wt"); time(&t1); *stime = ctime(&t1); fprintf(fid,"*** pazul started : %s \n",*stime); Edit1->Text = *stime; Edit3->Text = IntToStr(time(&t2)-t1); Form1->Refresh(); exec(); tick2 = GetTickCount(); result = (int) (tick2 -tick1); time(&t2); *etime = ctime(&t2); fprintf(fid,"\n*** pazul ended time = %d ms : %s \n",result,*etime); Edit2->Text = *etime; Edit3->Text = IntToStr(t2-t1); Edit4->Text = IntToStr(result); fclose(fid); // reset cursor mark Form1->Cursor = crDefault; } //--------------------------------------------------------------------------- void TForm1::exec() { short k,n; for (N=2; N<=30; N++) { for (A=1; A<=N/2; A++) { B = N - A*2; // xnum[0] = 8; xnum[0] = 9; select(0); } } } //--------------------------------------------------------------------------- void TForm1::select(int m) { int i,j,k,n,d,w,bollow,snum1,snumx,X,half; if (m == A) { for (k=A+1; k<=A+B; k++) xnum[k] = 9; for (k=1; k<=A; k++) xnum[N-k+1] = 9 - xnum[k]; xnum[A]--; xnum[N]++; for (n=0; n<=9; n++) digit[n] = 0; for (k=1; k<=N; k++) digit[xnum[k]]++; X = -1; if (digit[0] != 0) { X = digit[0] + 1; snum1 = xnum[1]; snumx = xnum[X]; if (X >= A) goto exit; for (k=1; k<10; k++) if (digit[k] != 0) break; // printR("before ",xnum); digit[xnum[1]]--; digit[xnum[X]]--; half = xnum[1] >> 1; if (xnum[1] >= k*2) { xnum[1] -= k; xnum[X] += k; } else { if (xnum[1] & 0x01) goto exit; xnum[1] = half; xnum[X] += half; } if (xnum[X] > 9) goto exit; digit[xnum[1]]++; digit[xnum[X]]++; } n = 0; for (d=0; d<=9; d++) for (k=1; k<=digit[d]; k++) onum[++n] = d; if (n != N) { fprintf(fid,"++++bugbugbug+++\n"); fflush(fid); } for (k=1; k<=N; k++) onum2[k] = onum[k]; if (X > 0) { onum[1] = onum[X]; onum[X] = 0; // printR("*** orxx : ",xnum); // printR("*** zero : ",onum); } bollow = 0; for (k=1; k<=N; k++) { if (onum[k] < onum[N-k+1] + bollow) { if (xnum[N-k+1] != (10+onum2[k]-onum[N-k+1]-bollow)) break; bollow = 1; } else { if (xnum[N-k+1] != (onum2[k]-onum[N-k+1]-bollow)) break; bollow = 0; } } if (k > N) { fprintf(fid,"\n***** Lucky !!!! N=%2d *****\n",N); printR("org ",xnum); printR("min ",onum); for (k=1; k<=N; k++) num[k] = onum2[N-k+1]; printR("max ",num); } exit: if (X > 0) { xnum[1] = snum1; xnum[X] = snumx; } xnum[A]++; xnum[N]--; return; } // for (k=2; k<=xnum[m]; k++) { for (k=1; k<=xnum[m]; k++) { xnum[m+1] = k; select(m+1); } } //--------------------------------------------------------------------------- void TForm1::printR(char id[],int num[]) { int k; fprintf(fid,"%s",id); for (k=1; k<=N; k++) fprintf(fid," %d",num[k]); fprintf(fid,"\n"); fflush(fid); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormClick(TObject *Sender) { main(); } //---------------------------------------------------------------------------