Diễn Đàn MathScope

Diễn Đàn MathScope (http://forum.mathscope.org/index.php)
-   Phần Mềm + Mã Nguồn (http://forum.mathscope.org/forumdisplay.php?f=120)
-   -   Lỗi tràn stack trong Free Pascal (http://forum.mathscope.org/showthread.php?t=40485)

hoang_kkk 24-01-2013 05:38 PM

Lỗi tràn stack trong Free Pascal
 
Anh chị em nào pro về tin học có thể chỉ giúp em vì sao code của chương trình này lại bị tràn stack không ạ :

Code:

program going_one_going_twice_gone;
const  max=1001;
        fi='cow.inp';
        fo='cow.out';
type    m1=array[0..max] of longint;
     
var    a:m1;
        m,n : integer;
        f:text;
        ok:m2;
        max_cost,tong_max:longint;
procedure input;
var      i : integer;
begin
  assign(f,fi); reset(f); readln(f,n,m);
  for i:=1 to m do
  readln(f,a[i]);
  close(f);
end;
procedure sort(l,r : integer);
var      i,j,tg,x : integer;
begin
  x:=a[(l+r) div 2];
  i:=l;
  j:=r;
  repeat
    while (a[i]<x) do inc(i);
    while (a[j]>x) do dec(j);
    if i<=j then
    begin
      tg:=a[i]; a[i]:=a[j]; a[j]:=tg;
      inc(i);
      dec(j);
    end;
    until i>j;
  if l<j then sort(l,j);
  if i<r then sort(i,r);
end;
procedure xuly_chinh;
var      i,j,k:integer;
begin
  assign(f,fo); rewrite(f);
  sort(1,m);
  fillchar(ok,sizeof(ok),false);
  i:=m+2;
  a[m+1]:=0;
  a[m+2]:=0;
  a[0]:=0;
  max_cost:=0;
  tong_max:=0;
  while a[i]+a[i-1]<=a[i]*m  do
  begin
    dec(i,2);
  max_cost:=a[i];
  tong_max:=max_cost*(m-i+1);
  end;
  write(f,max_cost,' ',tong_max);
  close(f);
end;
begin
  input;
  xuly_chinh;
end.

Nó bị lỗi tràn ở dòng
Code:

dec(i,2)

franciscokison 24-01-2013 10:20 PM

Trích:

Nguyên văn bởi hoang_kkk (Post 183401)
Anh chị em nào pro về tin học có thể chỉ giúp em vì sao code của chương trình này lại bị tràn stack không ạ :

Code:

program going_one_going_twice_gone;
const  max=1001;
        fi='cow.inp';
        fo='cow.out';
type    m1=array[0..max] of longint;
     
var    a:m1;
        m,n : integer;
        f:text;
        ok:m2;
        max_cost,tong_max:longint;
procedure input;
var      i : integer;
begin
  assign(f,fi); reset(f); readln(f,n,m);
  for i:=1 to m do
  readln(f,a[i]);
  close(f);
end;
procedure sort(l,r : integer);
var      i,j,tg,x : integer;
begin
  x:=a[(l+r) div 2];
  i:=l;
  j:=r;
  repeat
    while (a[i]<x) do inc(i);
    while (a[j]>x) do dec(j);
    if i<=j then
    begin
      tg:=a[i]; a[i]:=a[j]; a[j]:=tg;
      inc(i);
      dec(j);
    end;
    until i>j;
  if l<j then sort(l,j);
  if i<r then sort(i,r);
end;
procedure xuly_chinh;
var      i,j,k:integer;
begin
  assign(f,fo); rewrite(f);
  sort(1,m);
  fillchar(ok,sizeof(ok),false);
  i:=m+2;
  a[m+1]:=0;
  a[m+2]:=0;
  a[0]:=0;
  max_cost:=0;
  tong_max:=0;
  while a[i]+a[i-1]<=a[i]*m  do
  begin
    dec(i,2);
  max_cost:=a[i];
  tong_max:=max_cost*(m-i+1);
  end;
  write(f,max_cost,' ',tong_max);
  close(f);
end;
begin
  input;
  xuly_chinh;
end.

Nó bị lỗi tràn ở dòng
Code:

dec(i,2)

Bạn xem lại dòng
Code:

i:=m+2;
.


Múi giờ GMT. Hiện tại là 03:54 PM.

Powered by: vBulletin Copyright ©2000-2024, Jelsoft Enterprises Ltd.

[page compression: 8.32 k/8.76 k (5.01%)]