プログラム実行結果は以下の通り。
Find a:6, b:5, c:8, d:4, e:2, f:7, g:3, h:9
これを図にすると以下のようになる。
プログラムのソースは以下の通り。
#include "puzutl.h" YesNo sum19( int a, int b, int c) { if( a+b+c == 19) return YES; return NO; } YesNo used[10]; int main( int argc, cstring argv[]) { set<string> finded; for( int a=0; a< 10; a++) { used[a] = YES; for( int b=0; b< 10; b++) { if( used[b]) continue; used[b] = YES; for( int c=0; c< 10; c++) { if( used[c]) continue; used[c] = YES; for( int d=0; d< 10; d++) { if( used[d]) continue; used[d] = YES; for( int e=0; e< 10; e++) { if( used[e]) continue; used[e] = YES; for( int f=0; f< 10; f++) { if( used[f]) continue; used[f] = YES; for( int g=0; g< 10; g++) { if( used[g]) continue; used[g] = YES; for( int h=0; h< 10; h++) { if( used[h]) continue; used[h] = YES; if( sum19(a,b,c) && sum19(a,d,h) && sum19(c,d,f) && sum19(c,e,h) && sum19(f,g,h)) { char buf[128]; sprintf( buf, "%d,%d,%d,%d,%d,%d,%d,%d", a, b, c, d, e, f, g, h); if( finded.find(buf) == finded.end()) { // 重複チェックしてから出力する ps( "Find a:%d, b:%d, c:%d, d:%d, e:%d, f:%d, g:%d, h:%d\n", a, b, c, d, e, f, g, h); finded.insert(buf); sprintf( buf, "%d,%d,%d,%d,%d,%d,%d,%d", f, g, h, d, e, a, b, c); // abcとfghを入れ替えて鏡像を除去 finded.insert(buf); } } used[h] = NO; } used[g] = NO; } used[f] = NO; } used[e] = NO; } used[d] = NO; } used[c] = NO; } used[b] = NO; } used[a] = NO; } return 0; }