主题:救命啊~help me~~~!!
idolaigy
[专家分:0] 发布于 2005-12-23 13:45:00
我们老师今天出了一个题~
叫我们分别用三种循环语句算出1到100之间的所有偶数的和~同学用除2取余的方法算出来了~
我自己想用用2的倍数算出来~我用while语句写出来了~可是用for和repeat怎么也写不出来,请教一下大家是哪有问题~小弟先谢了~~
我的代码是这样写的~
program abc (input,output);
var
x,y,z:integer;
begin
y:=0;
z:=0;
for x:=1 to 100 do
begin
if (z<=100)
then y:=y+z;
z:=x*2;
x:=x+1;
end;
writeln(y);
end.
program abc (input,output);
var
x,y,z:integer;
begin
y:=0;
z:=0;
repeat
if (z<=100)
then y:=y+z;
z:=x*2;
x:=x+1;
until x<=100;
writeln(y);
end.
回复列表 (共6个回复)
沙发
lmj9201 [专家分:1400] 发布于 2005-12-23 20:19:00
这是FOR循环的
program abc;
var
a,b:integer;
begin
b:=0;
for a:=1 to 50 do
b:=b+2*a;
write(b);
end.
这是你编的,我为你找点毛病
program abc (input,output);
var
x,y,z:integer;
begin
y:=0;
z:=0;
for x:=1 to 100 do
begin
if (z<=100) [color=00FFFF]{这个if语句没用,可去掉}[/color]
then y:=y+z;
z:=x*2;
x:=x+1;[color=00FFFF]{'x:=x+1;'这个语句也错了,因为上面你已经在累加了,for循环就是累加,所以应删掉}[/color]
end;
writeln(y);
end.
while循环的
program abc;
var
a,b:integer;
begin
b:=0;
while a<=100 do
begin
b:=b+a;
a:=a+2;
end;
write(b);
end.
这是repeat循环的
program abc;
var
a,b:integer;
begin
a:=0;b:=0;
repeat
a:=a+b;b:=b+2;
until b=102;
end.
板凳
lmj9201 [专家分:1400] 发布于 2005-12-23 20:20:00
能加分吗?
3 楼
blackmark [专家分:210] 发布于 2005-12-25 02:41:00
program shu;
var
x1,x2:integer;
fout:text;
begin
assign(fout,'out.txt');
rewrite(fout);
x1:=2;
x2:=0;
reapt
x2:=x2+x1;
x1:=x1+2;
until x1:=102;
writeln(fout,x2);
end.
4 楼
blackmark [专家分:210] 发布于 2005-12-25 02:46:00
var
fout:text;
i,x1:integer;
begin
assign(fout,'out.txt');
rewrite(fout);
for i:=1 to 50 do
begin
x1:=x1+i*2;
end;
writeln(fout,x1);
end.
5 楼
blackmark [专家分:210] 发布于 2005-12-25 02:53:00
program shu;
var
x1,x2:integer;
fout:text;
begin
assign(fout,'out.txt');
rewrite(fout);
x1:=2;
x2:=0;
reapt
x2:=x2+x1;
x1:=x1+2;
until x1:=102;
writeln(fout,x2);
close(fout);
end.
6 楼
blackmark [专家分:210] 发布于 2005-12-25 02:54:00
var
fout:text;
i,x1:integer;
begin
assign(fout,'out.txt');
rewrite(fout);
for i:=1 to 50 do
begin
x1:=x1+i*2;
end;
writeln(fout,x1);
close(fout);
end.
我来回复