朝日新聞2004年7月10日パズルパーク解答

プログラムの実行結果は以下の通り。

15/9 + 2/3 + 4/8 + 7/6
15/9 + 2/3 + 7/6 + 4/8
15/9 + 4/8 + 2/3 + 7/6
15/9 + 4/8 + 7/6 + 2/3
15/9 + 7/6 + 2/3 + 4/8
15/9 + 7/6 + 4/8 + 2/3
cnt:362880

並び順を無視すれば上記式は同一になる。

ソースは以下の通り。

#include "puzutl.h"

YesNo used[10];

int main( int argc, cstring argv[])
{
  int           cnt = 0;
  for( int a=1; a< 10; a++) {
    used[a] = YES;
    for( int b=1; b< 10; b++) {
      if( used[b]) continue;
      used[b] = YES;
      for( int c=1; c< 10; c++) {
        if( used[c]) continue;
        used[c] = YES;
        for( int d=1; d< 10; d++) {
          if( used[d]) continue;
          used[d] = YES;
          for( int e=1; e< 10; e++) {
            if( used[e]) continue;
            used[e] = YES;
            for( int f=1; f< 10; f++) {
              if( used[f]) continue;
              used[f] = YES;
              for( int g=1; g< 10; g++) {
                if( used[g]) continue;
                used[g] = YES;
                for( int h=1; h< 10; h++) {
                  if( used[h]) continue;
                  used[h] = YES;
                  for( int i=1; i< 10; i++) {
                    if( used[i]) continue;
                    used[i] = YES;
                    int x = (a*10+b)*e*g*i + d*c*g*i + f*c*e*i + h*c*e*g;
                    int y = c*e*g*i;
                    if( x / y == 4 && x % y == 0) {
                      ps( "%d%d/%d + %d/%d + %d/%d + %d/%d\n", a,b,c,d,e,f,g,h,i);
                    }
                    cnt++;
                    used[i] = NO;
                  }
                  used[h] = NO;
                }
                used[g] = NO;
              }
              used[f] = NO;
            }
            used[e] = NO;
          }
          used[d] = NO;
        }
        used[c] = NO;
      }
      used[b] = NO;
    }
    used[a] = NO;
  }
  ps( "cnt:%d\n", cnt);
  return    0;
}