怎么用 Pascal 的 repeat 语句读一组实数,遇零终止,打印其中正、负数的个数及各自的总和。

2024-12-16 05:39:46
推荐回答(2个)
回答1:

program ex;
  var
    n,sum1,sum2:real;
    x,y:integer;
  begin
    sum1:=0;
    sum2:=0;
    x:=0;
    y:=0;
    repeat
      read(n);
      if n>0 
        then
          begin
            x:=x+1;
            sum1:=sum1+n;
          end;
      if n<0
        then
          begin
            y:=y+1;
            sum2:=sum2+n;
          end;
    until n=0;
    writeln(x,sum1:0:2);
    writeln(y,xum2:0:2);
  end.

楼上的只限于整数,不包括实数,而且变量开得太大,占空间。

回答2:

var n,i,j:longint;
a,b:array[1..10000] of longint;
begin
i:=0;
j:=0;
repeat
read(n);
if n<0 then begin
inc(i);
a[i]:=n;
end
else if n>0 then begin
inc(j);
b[j]:=n;
end;
until n=0;
writeln(i);
for n:=1 to i do write(a[n],' ');
writeln;
writeln(j);
for n:=1 to j do write(b[n],' ');
end.