求N个整数的平均数、众数和中位数 用pascal,我只学过pascal
发布网友
发布时间:2022-04-23 01:33
我来回答
共4个回答
热心网友
时间:2023-10-10 05:42
program DoingData;
var
n : longint;
sum: longint;
n1 : real;
n2 : longint;
n3 : real;
a : array[0.. 1000] of longint;
f : array[0.. 100] of longint;
procere input();
var
i: longint;
begin
readln(n);
sum:= 0;
for i:= 1 to n do
begin
read(a[i]);
inc(f[a[i]]);
inc(sum, a[i]);
end;
end;
procere print_n1();
begin
n1:= sum / n;
write(n1: 0: 2, ' ');
end;
procere print_n2();
var
max: longint;
i : longint;
begin
max:= f[1];
for i:= 2 to n do
if f[i] > max then
begin
max:= f[i];
n2:= i;
end;
write(n2, ' ');
end;
procere print_n3();
var
mid1: longint;
mid2: longint;
procere qsort(s, e: longint);
var
i, j, mid: longint;
begin
i:= s;
j:= e;
mid:= a[(i + j) div 2];
while i <= j do
begin
while a[i] > mid do
inc(i);
while a[j] < mid do
dec(j);
if i <= j then
begin
a[0]:= a[i];
a[i]:= a[j];
a[j]:= a[0];
inc(i);
dec(j);
end;
end;
if i < e then qsort(i, e);
if j > s then qsort(s, j);
end;
begin
qsort(1, n);
if odd(n) then
writeln(a[(1 + n) div 2])
else
begin
mid1:= a[(1 + n) div 2];
mid2:= a[(1 + n) div 2 + 1];
n3:= (mid1 + mid2) / 2;
writeln(n3: 0: 1);
end;
end;
begin
input;
print_n1;
print_n2;
print_n3;
end.
热心网友
时间:2023-10-10 05:42
var
a:array[0..10002] of integer;
num:array[0..102] of integer;
i,res2,n,sum,aa,max,nn,mm:longint;
res3,res1:real;
procere sort(l,r: longint);
var
i,j,x,y: longint;
begin
i:=l;
j:=r;
x:=a[(l+r) div 2];
repeat
while a[i]<x do
inc(i);
while x<a[j] do
dec(j);
if not(i>j) then
begin
y:=a[i];
a[i]:=a[j];
a[j]:=y;
inc(i);
j:=j-1;
end;
until i>j;
if l<j then
sort(l,j);
if i<r then
sort(i,r);
end;
begin
read(n);
sum:=0;aa:=0;max:=0;nn:=0;mm:=0;
for i:=1 to n do begin
read(a[i]);
if(a[i]>max) then max:=a[i];
inc(num[a[i]]);
sum:=sum+a[i];
end;
res1:=sum/n;
for i:=1 to max do begin
if(num[i]>nn)then begin
nn:=num[i];
mm:=i;
end;
end;
res2:=num[mm];
sort(1,n);
if(n mod 2=0) then res3:=(a[n div 2]+a[n div 2+1])/2
else res3:=a[n div 2+1];
write(res1:0:2,' ',res2,' ',res3:0:1);
end.
试试
热心网友
时间:2023-10-10 05:43
var
a:array[0..10002] of integer;
num:array[0..102] of integer;
i,res2,n,sum,aa,max,nn,mm:longint;
res3,res1:real;
procere sort(l,r: longint);
var
i,j,x,y: longint;
begin
i:=l;
j:=r;
x:=a[(l+r) div 2];
repeat
while a[i]<x do
inc(i);
while x<a[j] do
dec(j);
if not(i>j) then
begin
y:=a[i];
a[i]:=a[j];
a[j]:=y;
inc(i);
j:=j-1;
end;
until i>j;
if l<j then
sort(l,j);
if i<r then
sort(i,r);
end;
begin
read(n);
sum:=0;aa:=0;max:=0;nn:=0;mm:=0;
for i:=1 to n do begin
read(a[i]);
if(a[i]>max) then max:=a[i];
inc(num[a[i]]);
sum:=sum+a[i];
end;
res1:=sum/n;
for i:=1 to max do begin
if(num[i]>nn)then begin
nn:=num[i];
mm:=i;
end;
end;
res2:=num[mm];
sort(1,n);
if(n mod 2=0) then res3:=(a[n div 2]+a[n div 2+1])/2
else res3:=a[n div 2+1];
write(res1:0:2,' ',res2,' ',res3:0:1);
end.追问还是错的,不信你看:
运行编号 用户 问题 结果 内存 耗时 语言 代码长度 提交时间
471691 pengbo 1180 答案错误 244 16 Pascal/Edit 1334 B 2012-01-25 07:47:42
追答……把网址给我,我调对了再给你
热心网友
时间:2023-10-10 05:43
readln(n);
for :=1 to n do begin read(a[i]);s:=s+a[i];inc(q[a[i]]);end;
write(s/n:0:2,' ');
排序a数组;
排序q数组;
max:=0;
for i:=1 to 100 do if q[max]<q[i] then max:=i;
write(max,' ')
if n mod 2=0 then write((a[n div 2]+a[n div 2+1])/2:0:1)
else write(a[n div 2+1]);
end;