先预处理0--2000所有数字的笔画数。
然后枚举所有情况。可以用数学证明不会出现2000以上的数字。
用一个双重循环实现
for i:=0 to 1000 do
for j:=0 to 1000 do
if data[i]+data[j]+data[i+j]=n then inc(tot);
附程序:
const bh:array ['0'..'9'] of longint=(6,2,5,5,4,5,6,3,7,6);
var data:array[0..2000] of longint;
i,j,n,tot:longint;st:string;
{main}
begin
fillchar(data,sizeof(data),0);
tot:=0;
for i:=0 to 2000 do
begin
str(i,st);
for j:=1 to length(st) do inc(data[i],bh[st[j]]);
end;
readln(n);
dec(n,4);
for i:=0 to 1000 do
for j:=0 to 1000 do
if data[i]+data[j]+data[i+j]=n then inc(tot);
writeln(tot);
end.
这道题NOIP时,我是搜出来怕超时然后打表的.
思路:枚举所有等式,计算火柴数,相等就inc(ans),注意优化!!!
好难