/* ******************************************************************************  数学の部屋・今週の問題『第164回』 1□□2 □  □ 右の□に5,6,7,8,9,10,11,12の数字を一つずつ入れていき、 □  □ 各辺の合計が等しくなるようにする。 3□□4 +−+−+−+−+ |1|a|b|2| +−+−+−+−+ |c|   |g| +−+   +−+ |d|   |h| +−+−+−+−+ |3|e|f|4| +−+−+−+−+ ●【注意】  このプログラムを実行させると、ディレクトリ内に「no164out.txt」ファイル  が作成されます。 ****************************************************************************** */ #include ; #include ; #include ; void sub_analyze(void); void sub_analyze2(void); int fnc_is_analyze_ok(void); /* (定数) */ #define OUT_FILE_NAME "no164out.txt" #define START_NUMBER 12345678 #define END_NUMBER 87654321 #define TRUE -1 #define FALSE 0 /* (変数) */ int cnt = 0; /* パターンの数 */ int len; /* 四辺すべてが等しいときの一辺の合計 */ int a,b,c,d,e,f,g,h; /* a〜h の変数 */ int l1,l2,l3,l4; /* 各一辺の合計数 */ char num[10]; /* 12345678 〜 87654321 */ FILE *fp; /* (処理)main() */ int main() { fp = fopen (OUT_FILE_NAME,"w"); /* 出力用ファイルを開く。 */ if(fp != NULL){ fprintf(fp,"+−+−+−+−+ \n"); fprintf(fp,"|1|a|b|2| \n"); fprintf(fp,"+−+−+−+−+ \n"); fprintf(fp,"|c|   |g| \n"); fprintf(fp,"+−+   +−+ \n"); fprintf(fp,"|d|   |h| \n"); fprintf(fp,"+−+−+−+−+ \n"); fprintf(fp,"|3|e|f|4| \n"); fprintf(fp,"+−+−+−+−+ \n"); fprintf(fp,"\n"); fprintf(fp,"\n"); sub_analyze(); fclose(fp); }else { printf("File Open Error! \n"); exit(1); } return 0; } /* (処理)解析処理 */ void sub_analyze() { unsigned long ul; for (ul = START_NUMBER ; ul <= END_NUMBER ; ul++){ ultoa(ul , num , 10); /* 数字が「1〜8」で構成されている場合、「解析処理2」 */ if (fnc_is_analyze_ok() == TRUE){ sub_analyze2(); } } } /* (処理)解析処理2 */ void sub_analyze2() { /* numには「1〜8」までの数字を使用した文字列があり、これを一桁ずつ分解して さらに、各数字に「4」を加算することで、a〜hまでに5〜12までの数字を設定する。 */ a = *(num) - 0x30 + 4; b = *(num+1) - 0x30 + 4; c = *(num+2) - 0x30 + 4; d = *(num+3) - 0x30 + 4; e = *(num+4) - 0x30 + 4; f = *(num+5) - 0x30 + 4; g = *(num+6) - 0x30 + 4; h = *(num+7) - 0x30 + 4; /* 四辺それぞれの長さを求める。 */ l1 = 1 + a + b + 2; l2 = 1 + c + d + 3; l3 = 3 + e + f + 4; l4 = 2 + g + h + 4; /* 四辺がすべて等しいか? */ if( (l1 == l2) && (l2 == l3) && (l3 == l4) ){ /* 四辺すべてが等しいとき、出力する。 */ len = l1; cnt++; printf("(No %3d) a=%2d , b=%2d , c=%2d , d=%2d , e=%2d , f=%2d , g=%2d , h=%2d (L=%2d)\n",cnt,a,b,c,d,e,f,g,h,len); fprintf(fp,"(No %3d) a=%2d , b=%2d , c=%2d , d=%2d , e=%2d , f=%2d , g=%2d , h=%2d (L=%2d)\n",cnt,a,b,c,d,e,f,g,h,len); } } /* (関数)8桁の数字が「1〜8」までの数字すべて使っているか */ int fnc_is_analyze_ok() { int i,j; int ret; ret = TRUE; /* 数字の各桁には「0」「9」は入らない。 */ for(i=0 ; i<8 ; i++){ if ( (num[i] == '0') || (num[i] == '9') ){ ret = FALSE; i=8; } } /* 数字の各桁には同じ数字が入らない。「例:12345677」 */ if (ret == TRUE){ for(i=0 ; i<8 ; i++){ for (j=0 ; j<8 ; j++){ if ( (i != j) && (num[i] == num[j]) ){ ret = FALSE; i=8; j=8; } } } } return ret; } /* ******************************************************************************* Copyright (c) 2002 F,tomohiro ******************************************************************************* */