Description
下面是一个乘法竖式,如果用我们给定的那几个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式。
* * *
x * *
-------
* * *
* * *
-------
* * * *
数字只能取代*,当然第一位不能为0。
写一个程序找出所有的牛式。
Input
Line 1:
数字的个数。
Line 2: N个用空格分开的数字(每个数字都∈{1,2,3,4,5,6,7,8,9})
。
Output
共一行,一个数字。表示牛式的总数。下面是样例的那个牛式。
2 2 2
x 2 2
------
4 4 4
4 4 4
---------
4 8 8 4
Sample Input
5
2 3 4 6 8
Sample Output
1
解题思路:读入n,用五重循环,穷举乘数与乘数五个位置,也就是1到n,判断每一个是否符合题目要求,如果符合,就把num+1,最后输出num即可。
程序:
var
n,i,i1,i2,temp,i4,i3,m1,m2,num:longint;
a:array[1..10]of longint;
function p1(v:longint):boolean;
var
x,i,t,j:longint;
q:boolean;
begin
x:=v;i:=0;
while x<>0 do
begin
t:=x mod 10;
j:=0;q:=false;
while j<=n do
begin
inc(j);
if t=a[j] then begin q:=true; break; end;
end;
if (q=false)or(t=0) then exit(false);
x:=x div 10;
inc(i);
end;
if (i=3)and(v<>0) then p1:=true else p1:=false;
end;
function p2(v:longint):boolean;
var
x,i,t,j:longint;
q:boolean;
begin
x:=v;i:=0;
while x<>0 do
begin
t:=x mod 10;
j:=0;q:=false;
while j<=n do
begin
inc(j);
if t=a[j] then begin q:=true;break; end;
end;
if (q=false)or(t=0) then exit(false);
x:=x div 10;
inc(i);
end;
if (i=4)and(v<>0) then p2:=true else p2:=false;
end;
begin
readln(n);
for i:=1 to n do read(a[i]);
num:=0;
for i:=1 to n do
if a[i]<>0 then
for i1:=1 to n do
for i2:=1 to n do
for i3:=1 to n do
if a[i3]<>0 then
for i4:=1 to n do
begin
m1:=a[i]*100+a[i1]*10+a[i2];
m2:=a[i3]*10+a[i4];
if (p1(m1*a[i4]))and(p1(m1*a[i3]))and(p2(m1*m2)) then inc(num);
end;
writeln(num);
end.