输出1——999中能被3整除,且至少有一位数字是5的数。pascal编程

完整的Free Pascal语言编程
2024-12-01 11:14:39
推荐回答(4个)
回答1:

var i:integer;
begin
for i:=1 to 999 do
if (i mod 3=0)and
((i mod 10=5) or (i div 10 mod 10=5) or (i div 100=5))
then write(i:10);
end.

回答2:

前面复杂度为O(999),其实可以再快一点:
program div;
var i1,i2:integer;
begin
for i:=1 to 333 do
begin
i2:=3*i;
if (i2 div 100=5) or ((i2-(i2 div 100 * 100)) mod 10=5) or (i2 mod 10=5) then write(i2:5):
end;
readln;
end.

回答3:

var s:string;
i,j:integer;
begin
for i:=1 to 999 do
if i mod 3=0 then
begin
str(i,s);
for j:=1 to 3 do
if s[j]='5' then
writeln(i);
end;
end.

回答4:

var i1,i2:integer;
begin
for i:=1 to 333 do
begin
i2:=3*i;
if (i2 div 100=5) or ((i2-(i2 div 100 * 100)) mod 10=5) or (i2 mod 10=5) then write(i2:5):
end;
readln;
end.