pascal 输入n,输出1~n中有几个5,几个2

来源:百度知道 编辑:UC知道 时间:2024/05/10 08:17:57
输入n,输出1~n中有几个5,几个2

这是我写的,都对,便于理解,有注释,希望能帮助你。
var
n,i,a,wu,er:integer; {integer是整型}
begin
readln(n); {读入n}
for i:=1 to n do {for语句,即从1到n每次都执行下面的语句}
begin
read(a); {读入这个数}
if a=5 then inc(wu); {if语句,即如果这个数是5就给wu这个变量加1,inc就是累加函数}
if a=2 then inc(er); {同样道理,如果这个数是2就给er这个变量加1}
end;
writeln('5:',wu); {输出5有几个}
writeln('2:',er); {输出2有几个}
end.
要加分哦~
O(∩_∩)O~

for i:=1 to n do
begin
str(i,s);
for j:=1 to length(s) do
begin
if s[j]='5' then
inc(s5);
if s[j]='2' then
inc(s2);
end;
end;
writeln(s5);
writeln(s2);

Program e1;
Var
s: string;
i, j, l, n, i2, i5: longint;
Begin
Readln(n);
For i:=1 to n do Begin
str(i, s);
l := length(s);
For j:=1 to l do Begin
If