讨论 / 大牛能告诉我 错在哪里 怎么只过了四个数据
李宏光 2012-04-28 07:18:00
点我顶贴 收藏 删除
var

i,n:longint;

g,f:array[-2..100001]of longint;

h:array[1..100]of longint;

function min1 (a,b:longint):longint;

begin

min1:=a;

if b<a then min1:=b;

end;

begin

read(n);

for i:=1 to n do read(h[i]);

for i:=1 to n do

begin

f[i]:=min1(g[i-1],g[i-2]);

g[i]:=min1(f[i-1]+h[i],g[i-1]+h[i]);

end;

write(min1(f[n],g[n]));

end.

哪位 大牛能告诉我 错在哪里 怎么只过了四个数据

#1 王昱炜@2012-04-28 07:18:00
回复 删除
var f:array[-1..10001,0..1] of longint;

a:array[0..10001] of byte;

var i,n,j,min:longint;

begin

read(n);

for i:=-1 to n do

begin

f[i,0]:=maxint;

f[i,1]:=maxint;

end;

for i:=1 to n do read(a[i]);

f[0,1]:=0;

f[-1,1]:=0;

for i:=1 to n do

begin

if f[i-1,1]>f[i-1,0] then f[i,1]:=f[i-1,0]+a[i] else f[i,1]:=f[i-1,1]+a[i];

if f[i-1,1]>f[i-2,1] then f[i,0]:=f[i-2,1] else f[i,0]:=f[i-1,1];

end;

if f[n,1]>f[n,0] then min:=f[n,0] else min:=f[n,1];

write(min);

end.

查看更多回复
提交回复