當前位置:成語大全網 - 新華字典 - 姓名按字典排序 pascal編程

姓名按字典排序 pascal編程

var

i,n:Longint;

a:array[0..1000] of string;

procedure sort(i,j:longint);

var

l,r:longint;

m:string;

begin

l:=i;

r:=j;

m:=a[(i+j) div 2];

repeat

while a[i]<m do inc(i);

while a[j]>m 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;

until i>j;

if i<r then sort(i,r);

if l<j then sort(l,j);

end;

begin

readln(n);

for i:=1 to n do

readln(a[i]);

sort(1,n);

for i:=1 to n do

writeln(a[i]);

end.

如果題目中連名字總數(n)都沒有,

妳可以把讀入部分換成

while not eof do

begin

inc(i);

readln(a[i]);

end;

eof 是文件結束標誌,用文件輸入輸出處理好些:

begin

assign(input,'文件名.in');reset(input);

assign(output,'文件名.out');rewrite(output);

.

.

close(input);

close(output);

end.

我用的是快速排序,

如果妳要交題的話請註意壹下數組範圍。

還有壹點,程序中數組範圍也可改到1到1000,然後把a[0]改成另壹個閑置的變量;不過數組開多幾位是好習慣,避免201錯誤。