讨论 / Pascal解
Heartbeat 2015-05-24 17:04:22
点我顶贴 收藏 删除
var a:array[1..10000] of longint;

n,m,i,k,e,t:longint;

begin

readln(n);

readln(m);

for i:=1 to n do

read(a[i]);

for m:=m downto 1 do

begin

k:=n;

while a[k-1] > a[k] do

dec(k);

e:=n;

while a[k-1] > a[e] do

dec(e);

t:=a[k-1];

a[k-1]:=a[e];

a[e]:=t;

e:=n;

while k < e do

begin

t:=a[k];

a[k]:=a[e];

a[e]:=t;

inc(k);

dec(e);

end;

end;

for i:=1 to n do

write(a[i],' ');

end.

#1 潘崇明@2016-02-12 15:34:39
回复 删除
你这是错的,我这才是对的

var n,m,i,x,q,left,right,t,max,j:longint;

a:array[1..10000]of longint;

begin

{assign(input,'martian.in');

assign(output,'martian.out');

reset(input);

rewrite(output);}

read(n,m);

for i:=1 to n do

read(a[i]);

for i:=1 to m do

begin

x:=n-1;

while a[x]>a[x+1] do

x:=x-1;

max:=maxlongint;

t:=0;

for j:=x+1 to n do

if (a[j]>a[x])and(a[j]<max)

then begin

max:=a[j];

t:=j;

end;

q:=a[x];

a[x]:=a[t];

a[t]:=q;

left:=x+1;

right:=n;

while left<right do

begin

q:=a[left];

a[left]:=a[right];

a[right]:=q;

left:=left+1;

right:=right-1;

end;

end;

for i:=1 to n-1 do

write(a[i],' ');

writeln(a[n]);

{close(input);

close(output);}

end.

查看更多回复
提交回复