广州科学城旭丽电子厂:递归的问题(pascal的)

来源:百度文库 编辑:中科新闻网 时间:2024/04/28 23:18:30
输入一个数,输出零至这个数中含有”3”的数字的个数
(用递归,不要用字符串)

var
i,n,tot:longint;

function include3(x:longint):boolean;
var
t:integer;
begin
if x<10 then
begin
if x=3 then include3:=true
else include3:=false;
end
else
begin
t:=x mod 10;
if t=3 then
begin
include3:=true;
exit;
end
else include3:=include3(x div 10);
end;
end;

begin
readln(n);
tot:=0;
for i:=0 to n do
if include3(i) then inc(tot);
writeln(tot);
end.

include3就是一个递归.
不算难懂我就不解释了.
不过为什么非递归不可呢?用循环强多了!