模拟试——match

来源:互联网 发布:网易养猪场知乎 编辑:程序博客网 时间:2024/06/03 11:17

题目大意:
这里写图片描述
以上是每个数需要的火柴数。
火柴分为A,B,C三堆,然后用光各拼出一个数,分别是a,b,c,这三个数都不能有前导0(0本身除外),再选择一个运算符op(+,-,*,/,其中/是整数除),使得a op b = c。因为小明只学过0到999的数,所以只能拼0到999这1000个数。
求最后能拼出多少种这样的等式。

对于所有的数据,2 ≤ A,B,C ≤ 21

题解:
搜索,把3种火柴能摆的所有情况全部暴力搜出来,因为最高只能拼成3位数即0~999可以拼,所以我们搜到4位数就exit。
最后强行枚举。
PS:不能除0.

const     num:array [0..9] of longint=(6,2,5,5,4,5,6,3,7,6);var     cd:array [1..3,0..999] of boolean;     i,j,a,b,c,ans:longint;procedure dfs(k,ks,now,g:longint);var     i:longint;begin     if now>999 then exit;     if ks=g then     begin          cd[k,now]:=true;          exit;     end;     if (now<>0) or (num[0]=g) then dfs(k,ks+num[0],now*10,g);     for i:=1 to 9 do         if ks+num[i]<=g then dfs(k,ks+num[i],now*10+i,g);end;function check(l,r:longint):longint;begin     check:=0;     if l-r>=0 then        if cd[3,l-r] then inc(check);     if l+r<=999 then        if cd[3,l+r] then inc(check);     if l*r<=999 then        if cd[3,l*r] then inc(check);     if r<>0 then        if cd[3,l div r] then inc(check);end;begin      assign(input,'match.in'); reset(input);     assign(output,'match.out');rewrite(output);     readln(a,b,c);     dfs(1,0,0,a); dfs(2,0,0,b); dfs(3,0,0,c);     ans:=0;     for i:=0 to 999 do         if cd[1,i] then            for j:=0 to 999 do                if cd[2,j] then ans:=ans+check(i,j);     writeln(ans);     close(input); close(output);end.
原创粉丝点击