普及练习场之排序Ex

P1583 魔法照片
总:注意编号和权值。

var
  a,b,c:array [1..100001] of longint;
  n,i,k:longint;
procedure qsort(l,r:longint);
var
  i,j,t,mid1,mid2:longint;
begin
  i:=l; j:=r;
  mid1:=a[(l+r) div 2];
  mid2:=c[(l+r) div 2];
  repeat
    while (a[i]>mid1) or (a[i]=mid1) and (c[i]<mid2) do inc(i);
    while (a[j]<mid1) or (a[j]=mid1) and (c[j]>mid2) do dec(j);
    if i<=j then
      begin
        t:=a[i]; a[i]:=a[j]; a[j]:=t;
        t:=c[i]; c[i]:=c[j]; c[j]:=t;
        inc(i); dec(j);
      end;
  until i>j;
  if l<j then qsort(l,j);
  if i<r then qsort(i,r);
end;

begin
  read(n,k);
  for i:=1 to 10 do
    read(b[i]);
  for i:=1 to n do
    begin
      c[i]:=i; read(a[i]);
    end;
  qsort(1,n);
  for i:=1 to n do
    a[i]:=a[i]+b[(i-1) mod 10+1];
  qsort(1,n);
  for i:=1 to k do
    write(c[i],' ');
end.

P1051 谁拿了最多奖学金
总:处理一下结构,排序即可。

var
  a,e,d:array[1..1000]of string;
  b,c,f,g:array[1..1000]of longint;
  n,k,i,j,max,ans:longint;
  s:string;
begin
  readln(n);
  for i:=1 to n do
    begin
      readln(s);
      k:=pos(' ',s); a[i]:=copy(s,1,k-1); delete(s,1,k);
      k:=pos(' ',s); val(copy(s,1,k-1),b[i]); delete(s,1,k);
      k:=pos(' ',s); val(copy(s,1,k-1),c[i]); delete(s,1,k);
      k:=pos(' ',s); d[i]:=copy(s,1,k-1); delete(s,1,k);
      k:=pos(' ',s); e[i]:=copy(s,1,k-1); delete(s,1,k);
      val(copy(s,1,k-1),f[i]);
      if (b[i]>80)and(f[i]>0) then g[i]:=g[i]+8000;
      if (b[i]>85)and(c[i]>80) then g[i]:=g[i]+4000;
      if (b[i]>90) then g[i]:=g[i]+2000;
      if (b[i]>85)and(e[i]='Y') then g[i]:=g[i]+1000;
      if (c[i]>80)and(d[i]='Y') then g[i]:=g[i]+850;
    end;
  max:=0;
  for i:=1 to n do
    if max<g[i] then
      begin
        max:=g[i]; j:=i;
      end;
  writeln(a[j]);
  writeln(g[j]);
  for i:=1 to n do
    ans:=ans+g[i];
  writeln(ans);
end.

P1093 奖学金
总:你只有300,多关键字冒泡排序即可。

var
  a,b,c:array [1..300] of longint;
  i,j,k,n,m,x,y,t,t1,t2:longint;
begin
  read(n);
  for i:=1 to n do
    begin
      read(b[i],x,y);
      a[i]:=b[i]+x+y;
      c[i]:=i;
    end;
  for i:=1 to n-1 do
   for j:=i+1 to n do
     if (a[i]<a[j]) or ((a[i]=a[j]) and (b[i]<b[j])) or ((a[i]=a[j]) and (b[i]=b[j]) and (c[i]>c[j])) then
       begin
         t:=a[i]; t1:=b[i]; t2:=c[i];
         a[i]:=a[j]; b[i]:=b[j]; c[i]:=c[j];
         a[j]:=t; b[j]:=t1; c[j]:=t2;
       end;
  for i:=1 to 5 do
    writeln(c[i],' ',a[i]);
end.
原文地址:https://www.cnblogs.com/zyx-crying/p/9319515.html