free pascal编程:给出n个数,你要将这n个数从小到大排序输出,源程序如下,var a:array[1..10] of longint; i,j,t,n:longint;begin readln(n); for i:=1 to n do read(a[i]); for i:=1 to n-1 do for j:=i+1 to n do begin if a[i]>a[j] then b
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/06 04:20:50
free pascal编程:给出n个数,你要将这n个数从小到大排序输出,源程序如下,var a:array[1..10] of longint; i,j,t,n:longint;begin readln(n); for i:=1 to n do read(a[i]); for i:=1 to n-1 do for j:=i+1 to n do begin if a[i]>a[j] then b
free pascal编程:给出n个数,你要将这n个数从小到大排序输出,源程序如下,
var
a:array[1..10] of longint;
i,j,t,n:longint;
begin
readln(n);
for i:=1 to n do
read(a[i]);
for i:=1 to n-1 do
for j:=i+1 to n do
begin
if a[i]>a[j] then
begin
t:=a[j]; a[j]:=a[i]; a[i]:=t;
end;
end;
for i:=1 to n do
writeln(a[i]);
end.下面有段话,其实该程序还可以进一步优化,再为第i位选择一个最大的数时,可以在[i,n]范围内先选好最大的数,最后进行一次交换,这样减少了很多交换次数.,什么在为第i为选择一个最大得数啊?还再[i,n]先选好,.
free pascal编程:给出n个数,你要将这n个数从小到大排序输出,源程序如下,var a:array[1..10] of longint; i,j,t,n:longint;begin readln(n); for i:=1 to n do read(a[i]); for i:=1 to n-1 do for j:=i+1 to n do begin if a[i]>a[j] then b
var
a:array[1..10] of longint;
i,j,t,n:longint;
max:longint; {增加一个临时变量,用其保存最大的序号}
begin
readln(n);
for i:=1 to n do
read(a[i]);
for i:=1 to n-1 do begin
max:=i; {先假设下标为i的元素为最大}
for j:=i+1 to n do if a[j]>a[max] then max:=j; {如果找到比假设大的,记下它的下标}
if maxi then {只有当循环后的max不等于i时才交换}
begin
t:=a[i]; a[i]:=a[max]; a[max]:=t;
end;
end;
for i:=1 to n do
writeln(a[i]);
end.