FreePascal问题:
用递归方法求n个数中的最大值及其位置。
FreePascal问题:
用递归方法求n个数中的最大值及其位置。
递归
二分查找,分别取最大进行比较
打擂台就可以,给一个程序段,假设a数组中存数,共n个数,读入自行解决 max存最大,w存位置
var
i,n,max,w:longint;
a:array[1..1000] of longint;
begin
max:=0;
for i:=1 to n do if a[i]>max then
begin
max:=a[i]; w:=i;
end;
writeln(max,w);
end.
人家要你递归你就递归累= =
给思路:
设a数组存着n个待比较的数
然后从一半开始分
每一段如果数目大于2就再分成两段.,如果只有2个或一个,则返回较大值
下面给程序
var a:array[1..100] of longint; i,n,j:integer;
function com(l,m:integer):integer; begin if a[l]>a[m] then exit(l) else exit(m); end;
function max(x,y:integer):longint; var i:integer; begin if y=x+1 then exit(com(x,y)) else if y=x then exit(x); i:=(x+y) div 2; exit(com(max(x,i),max(i+1,y))); end;
begin readln(n); for i:=1 to n do read(a[i]); j:=max(1,n); writeln('No.',j,' ',a[j]); end.