Section 1.4 Prime Cryptarithm

来源:互联网 发布:淘宝网晚礼服女 编辑:程序博客网 时间:2024/05/16 07:10

由于数据足够小 只有枚举并判断就行了 由于要多次判断 不用函数将很麻烦

{
ID: yaoyuan4
PROG: crypt1
LANG: PASCAL
}
Program crypt1;
const
  inf = 'crypt1.in'; outf = 'crypt1.out';
var
  n, ans : longint;
  f : array[0..9] of boolean;
Procedure init;
  var
   i, x : longint;
  begin
   fillchar(f, sizeof(f), false);
   ans := 0;
   assign(input, inf); reset(input);
   readln(n);
   for i := 1 to n do
    begin
     read(x);
     f[x] := true;
    end;
   close(input);
  end;
Function ok(t : longint) : boolean;
  var
   x : longint;
  begin
   repeat
    x := t mod 10;
    t := t div 10;
    if not(f[x]) then exit(false);
   until t = 0;
   exit(true);
  end;
Procedure work;
  var
   a, b, c, d, e : longint;
  begin
   for a := 111 to 999 do
    for b := 11 to 99 do
     begin
      c := a * (b mod 10);
      d := a * (b div 10);
      e := a * b;
      if (c < 1000) and (d < 1000) and (e < 10000) then
       if (ok(a)) and (ok(b)) and (ok(c)) and (ok(d))

and (ok(e)) then
        inc(ans);
     end;
  end;
Procedure print;
  begin
   assign(output, outf); rewrite(output);
   writeln(ans);
   close(output);
  end;
begin
  init;
  work;
  print;
end.

原创粉丝点击