您的位置:首页 > 其它

USACO 1.3 牛式

2016-04-08 21:43 393 查看
Description下面是一个乘法竖式,如果用我们给定的那几个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式。   * * *  x  * *  -------   * * *  * * *  -------  * * * *数字只能取代*,当然第一位不能为0。写一个程序找出所有的牛式。InputLine 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 4Sample 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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: