[题目描述]
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.