排序算法_众数
AYYZOJ p1436
分析:桶排序,直接统计,可能有多个众数。
1 program p1436; 2 var 3 n,i,max:longint; 4 a:array[1..30000] of longint; 5 begin 6 max:=-maxint; 7 readln(n); 8 for i:=1 to n do 9 begin 10 read(n); 11 inc(a[n]); 12 end; 13 for i:=1 to 30000 do 14 if a[i]>0 then 15 if a[i]>max then max:=a[i]; 16 for i:=1 to 30000 do 17 if a[i]=max then writeln(i,' ',a[i]); 18 end.
1 var 2 a:array[1..30000] of integer; 3 max,i,j,n,m:integer; 4 begin 5 readln(n); 6 max:=0; 7 for i:=1 to n do 8 begin 9 read(m); 10 inc(a[m]); 11 if max<a[m] then max:=a[m]; 12 end; 13 for i:=1 to 30000 do 14 if a[i]=max then writeln(i,' ',a[i]); 15 end.
排序算法_军事机密
AYYZOJ p1438
分析:数据规模万级的,用快排或归并排序
想当初我真是太天真了,给你们看看我的冒泡80分程序:
1 program p1438; 2 var 3 a,b:array[1..30000] of longint; 4 n,i,j,t,k:longint; 5 flag:boolean; 6 begin 7 readln(n); 8 for i:=1 to n do read(a[i]); 9 readln(k); 10 for i:=1 to k do readln(b[i]); 11 for j:=1 to n-1 do begin 12 flag:=true; 13 for i:=1 to n-j do 14 if a[i]>a[i+1] then begin 15 t:=a[i];a[i]:=a[i+1];a[i+1]:=t; 16 flag:=false; 17 end; 18 if flag then break; 19 end; 20 t:=0; 21 repeat 22 inc(t); 23 writeln(a[b[t]]); 24 until t=k; 25 end.
1 var 2 a:array[1..30000] of longint; 3 n,i,k,m:longint; 4 procedure qsort(l,r:longint); 5 var 6 tmp,i,j,mid:longint; 7 begin 8 i:=l; 9 j:=r; 10 mid:=a[(i+j)div 2]; 11 repeat 12 while a[i]<mid do inc(i); 13 while a[j]>mid do dec(j); 14 if i<=j then 15 begin 16 tmp:=a[i];a[i]:=a[j];a[j]:=tmp; 17 inc(i); 18 dec(j); 19 end; 20 until i>j; 21 if l<j then qsort(l,j); 22 if i<r then qsort(i,r); 23 end; 24 begin 25 readln(n); 26 for i:=1 to n do read(a[i]); 27 qsort(1,n); 28 readln(k); 29 for i:=1 to k do begin 30 readln(m); 31 writeln(a[m]); 32 end; 33 end.
排序算法_输油管道
AYYZOJ p1439
分析:中位数问题,中位数(Median)统计学名词,是指将数据按大小顺序排列起来,形成一个数列,居于数列中间位置的那个数据。定义:一组数据按从小到大(或从大到小)的顺序依次排列,处在中间位置的一个数(或最中间两个数据的平均数)。
把油井按y坐标排序,求出序列中间位置的元素值,即管道铺设的位置,然后统计各油井到该管道的位置。(好像没有证明。。)
1 program p1439; 2 var 3 n,i,j:longint; 4 k,s:real; 5 a:array[1..100000] of longint; 6 procedure sort(l,r:longint); 7 var 8 tmp,mid:longint; 9 begin 10 i:=l; 11 j:=r; 12 mid:=a[(i+j)div 2]; 13 repeat 14 while a[i]<mid do inc(i); 15 while a[j]>mid do dec(j); 16 if i<=j then 17 begin 18 tmp:=a[i];a[i]:=a[j];a[j]:=tmp; 19 inc(i); 20 dec(j); 21 end; 22 until i>j; 23 if l<j then sort(l,j); 24 if i<r then sort(i,r); 25 end; 26 begin 27 readln(n); 28 for i:=1 to n do 29 readln(k,a[i]); 30 sort(1,n); 31 if odd(n) then k:=a[(n div 2+1)] 32 else k:=(a[(n div 2)]+a[(n div 2+1)])/2; 33 for i:=1 to n do 34 s:=s+abs(a[i]-k);writeln(trunc(s)); 35 end.
1 type 2 arr=array[1..10000] of integer; 3 var 4 x,y:arr; 5 ans:longint; 6 i,j,n,p:integer; 7 procedure sort(l,r:longint;var a:arr); 8 var 9 tmp,i,j,mid:longint; 10 begin 11 i:=l; 12 j:=r; 13 mid:=a[(i+j)div 2]; 14 repeat 15 while a[i]<mid do inc(i); 16 while a[j]>mid do dec(j); 17 if i<=j then 18 begin 19 tmp:=a[i];a[i]:=a[j];a[j]:=tmp; 20 inc(i); 21 dec(j); 22 end; 23 until i>j; 24 if l<j then sort(l,j,a); 25 if i<r then sort(i,r,a); 26 end; 27 begin 28 readln(n); 29 for i:=1 to n do readln(x[i],y[i]); 30 sort(1,n,y); 31 p:=y[n div 2+1]; 32 for i:=1 to n do 33 ans:=ans+abs(y[i]-p); 34 writeln(ans); 35 end.
排序算法_谁是组长
AYYZOJ p1442
COGS p979
分析:不能用桶排,n是longint。可以快排,然后统计得票最多的。
1 var 2 a:array[1..10000] of longint; 3 n,m,i,k,ans,max:longint; 4 procedure qsort(l,r:longint); 5 var 6 tmp,i,j,mid:longint; 7 begin 8 i:=l; 9 j:=r; 10 mid:=a[(i+j)div 2]; 11 repeat 12 while a[i]<mid do inc(i); 13 while a[j]>mid do dec(j); 14 if i<=j then 15 begin 16 tmp:=a[i];a[i]:=a[j];a[j]:=tmp; 17 inc(i); 18 dec(j); 19 end; 20 until i>j; 21 if l<j then qsort(l,j); 22 if i<r then qsort(i,r); 23 end; 24 begin 25 readln(n,m); 26 for i:=1 to m do read(a[i]); 27 qsort(1,m); 28 k:=1; max:=1; ans:=a[1]; 29 for i:=2 to m do begin 30 if a[i]=a[i-1] then 31 begin 32 inc(k); 33 if max<k then begin max:=k; ans:=a[i];end; 34 end 35 else k:=1; 36 end; 37 if max > m div 2 then writeln(ans) 38 else writeln(-1); 39 end.
这题WA了一次,原因是少了“k:=1;max:=1;ans:=a[1];”中的“ans:=a[1];”,其中有一个数据是 1 1 只有一人不赋初值导致输出0,还是考虑不周全啊。