buy low buy lower

来源:百度知道 编辑:UC知道 时间:2024/09/23 13:31:07
pascal编的
帮我看下问题出在哪

program dg_1;
var
a,b,c:array[1..5000] of integer;
i,s,n,d,j,l:longint;
begin
assign(input,'Buylow.in');reset(input);
assign(output,'Buylow.out');rewrite(output);
readln(n);
for i:=1 to n do
read(a[i]);
b[n]:=1;
for i:=n-1 downto 1 do
for j:=i+1 to n do
begin
if (a[i]>a[j]) and (b[i]<=b[j]) then b[i]:=b[j]+1;
if (a[i]<=a[j]) and (b[i]=0) and (j=n) then b[i]:=1;
if (b[i]<>1) and (j=n) then
for l:=i+1 to n do
if (b[l]+1=b[i]) and (a[i]>a[l]) then c[i]:=c[i]+c[l];
if b[i]=1 then c[i]:=1;
end;
s:=0;
for i:=1 to n do
if b[i]>s then s:=b[i];
write(s);l:=0;
for i:=1 to n do
if b[i]=s then l:=l+c[i];
write(' ',l);
close(input);close(output);
end.
b[n]:=1;后面还

你的这个算法比较诡异……应该是数据范围的问题,全部开到longint试试。
以下是我的程序,用了高精度:
program buylow(input,output);
const maxn=5000;
var price,best:array[0..5000]of longint;
methods:array[0..5000]of string;
i,j,n,max:longint;
function plus(s1,s2:string):string;
var a,b:array[1..70]of integer;
i,t,s,j:integer;
begin fillchar(a,sizeof(a),0);fillchar(b,sizeof(b),0);
for i:=70 downto (70-length(s1)+1) do
a[i]:=ord(s1[length(s1)-(70-i)])-48;
for i:=70 downto (70-length(s2)+1) do
b[i]:=ord(s2[length(s2)-(70-i)])-48;
t:=0;
for i:=70 downto 1 do
begin s:=a[i]+b[i]+t;
a[i]:=s mod 10;
t:=s div 10;
end;
i:=1;
while (a[i]=0)and(i<70) do inc(i);
plus:='';
for j:=i to 70 do
plus:=plus+chr(a[j]+48);
end;
begin assign(input,'buylow.in');reset(input);