[USACO 1.3.4] Prime Cryptarithm

[题目描述]

Prime Cryptarithm

牛式

下面是一个乘法竖式,如果用我们给定的那几个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式。

      * * *
   x    * *
    -------
      * * *
    * * *
    -------
    * * * *

数字只能取代*,当然第一位不能为0。

写一个程序找出所有的牛式。

PROGRAM NAME: crypt1

INPUT FORMAT

Line 1:

数字的个数。

Line 2:

N个用空格分开的数字(每个数字都∈{1,2,3,4,5,6,7,8,9}) 。

SAMPLE INPUT (file crypt1.in)

5
2 3 4 6 8

OUTPUT FORMAT

共一行,一个数字。表示牛式的总数。下面是样例的那个牛式。

      2 2 2
    x   2 2
     ------
      4 4 4
    4 4 4
  ---------
    4 8 8 4

SAMPLE OUTPUT (file crypt1.out)

1


[解题思路]

感觉和贪心没什么关系,5重循环的枚举就行了。

枚举两个数,分别看分步乘积,与最终答案是否为三位数和四位数,且数字在给定的集合内就行了。


[Code]

{
ID: zane2951
PROG: crypt1
LANG: PASCAL
}

program crypt1;
var
   a:array[0..11] of longint;
   f:array[0..11] of boolean;
   i,n,ans,i1,i2,i3,j1,j2:longint;

//-----------ko------------
function ko(x:longint):boolean;
var
   ce:longint;

begin
   while x>0 do
      begin
         ce:=x mod 10;
         if not f[ce] then exit(false);
         x:=x div 10;
      end;
   exit(true);
end;

//---------check-----------
function check(a,b,c:longint):boolean;
var
   ce:longint;

begin
   ce:=a*c;
   if (ce<100) or (ce>999) or not ko(ce) then exit(false);
   ce:=a*b;
   if (ce<100) or (ce>999) or not ko(ce) then exit(false);
   ce:=a*(10*b+c);
   if (ce<1000) or (ce>9999) or not ko(ce) then exit(false);
   exit(true);
end;

//----------main-----------
begin
   assign(input,'crypt1.in'); reset(input);
   assign(output,'crypt1.out'); rewrite(output);
   readln(n);
   for i:=1 to n do begin read(a[i]); f[a[i]]:=true; end;
   ans:=0;
   for i1:=1 to n do
      for i2:=1 to n do
         for i3:=1 to n do
            for j1:=1 to n do
               for j2:=1 to n do
                  if check(a[i1]*100+a[i2]*10+a[i3],a[j1],a[j2]) then inc(ans);
   writeln(ans);
   close(input); close(output);
end.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值