Free Pascal新手问题,错误代码201,最大和子序列,帮我看看错误(另外
1.设数组a是有n个元素的整数数组,从中找出最大和子序列(子序列是连续的)我的编程:programex5_2_7;c**tn=10;vara:array[1..n]ofi...
1.设数组a是有n个元素的整数数组,从中找出最大和子序列(子序列是连续的)我的编程:program ex5_2_7;c**tn=10;vara:array[1..n] of integer;b:array[1..n-1,1..3] of integer;i,j,k:integer;beginfor i:=1 to n doreadln(a[i]);for i:=1 to n-1 dobegink:=a[i];for j:=i+1 to n do begin if b[i,1]<k then beginb[i,1]:=k;b[i,2]:=i;b[i,3]:=j;end;k:=k+a[j];end;end;k:=b[1,1];for i:=1 to n-1 doif k<b[i,1] then begin k:=b[i,1]; j:=i;end; for i:=1 to 3 dowrite(b[j,i]:5);readln;end.
展开
1个回答
展开全部
不用贪心吗? program ex5_2_7; const n=10; var a:array[1..n] of integer; b:array[1..n-1,1..3] of integer; i,j,k:integer; begin for i:=1 to n do readln(a[i]); for i:=1 to n-1 do begin k:=a[i]; {应该有b[i,1]:=a[i]} for j:=i+1 to n do begin if b[i,1]<k then begin b[i,1]:=k; b[i,2]:=i; {这是废话} b[i,3]:=j; end; k:=k+a[j]; end; end; k:=b[1,1]; for i:=1 to n-1 do if k<b[i,1] then {b[j, 1]<b[i,1]即可,无需k} begin k:=b[i,1]; j:=i; end; for i:=1 to 3 do write(b[j,i]:5); readln; end.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询