朝日新聞2006年6月16日パズル横丁解答

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

A+B=C : OK
BX=VD : OK
ST+HF=SF+□□
ST(1920)+HF(86)=SF(196)+RJ(1810)=2006

合計値が2006ってところがミソかな。

文字は'A'⇒1,'B'⇒2,… というように割り当てる。

プログラムのソースは以下の通り。

#include "puzutl.h"

int c2i( char c) 
{
  return    c - 'A'+ 1;
}

int two( int x, int y)
{
  if( y < 10) return    x*10+y;
  return    x*100+y;
}

void rev_two( int v, char& c1, char& c2)
{
  if( v < 100) {
    c1 = (v/10)+'A'-1;
    c2 = (v%10)+'A'-1;
  }
  else {
    c1 = (v/100)+'A'-1;
    c2 = (v%100)+'A'-1;
  }
}

int main( int argc, cstring argv[])
{
  int a = c2i('A');
  int b = c2i('B');
  int c = c2i('C');
  int d = c2i('D');
  int s = c2i('S');
  int t = c2i('T');
  int h = c2i('H');
  int f = c2i('F');
  int x = c2i('X');
  int v = c2i('V');
  int bx = two(b,x);
  int vd = two(v,d);
  int st = two(s,t);
  int hf = two(h,f);
  int sf = two(s,f);
  ps( "A+B=C : ");
  if( a+b == c) ps( "OK\n"); else ps( "NG\n");
  ps( "BX=VD : ");
  if( bx == vd) ps( "OK\n"); else ps( "NG\n");
  ps( "ST+HF=SF+□□\n");
  int R = st+hf-sf;
  char c1, c2;
  rev_two( R, c1, c2);
  ps( "ST(%d)+HF(%d)=SF(%d)+%c%c(%d)=%d", st, hf, sf, c1, c2, R, st+hf);
  return    0;
}