program action;
const long_tab = 100;
type table = array[1..long_tab] of real;
procedure visu(t:table);
var i : integer;
begin
write('[');
for i:=1 to long_tab-1 do
begin
write(t[i]:3:2,' ; ')
end;
writeln(t[long_tab]:3:2,']')
end;
function amplitude(t:table):real;
var i : integer;
var min,max : real;
begin
min:=t[1];max:=t[1];
for i:=2 to long_tab do
begin
if t[i]<min then min:=t[i] else if t[i]>max then max:=t[i]
end;
amplitude:=max-min
end;
function gain(t:table):real;
var i,j, j_opt_i, j_tmp, i_tmp: integer;
var gain_tmp,gain_opt_i : real;
begin
gain_tmp:=0;
for i:=1 to long_tab do
begin
gain_opt_i:=0 ;
j_opt_i:=i ;
for j:=i+1 to long_tab do
begin
if t[j]-t[i]>gain_opt_i then
begin
gain_opt_i:=t[j]-t[i];
j_opt_i:=j
end;
end;
if gain_opt_i>gain_tmp then
begin
gain_tmp:=gain_opt_i;
j_tmp:=j_opt_i;
i_tmp:=i;
end
else
if gain_opt_i=gain_tmp then
if j_tmp - i_tmp > j_opt_i - i then
begin
j_tmp:=j_opt_i;
i_tmp:=i;
end
end;
writeln('gain obtenu avec un achat a ',i_tmp,' et une vente a ',j_tmp,'.');
gain:=gain_tmp;
end;
function gain1(t : table):real;
var i,i0,j0,k : integer;
var gain_tmp, gain_i : real;
begin
k:=1;gain_tmp:=0;
for i:=1 to long_tab do
begin
if t[i]<= t[k] then
begin
k:=i;
gain_i:=0
end
else
gain_i:=t[i]-t[k];
if gain_tmp<gain_i then
begin
gain_tmp:=gain_i;
j0:=i;
i0:=k;
end
else if gain_tmp=gain_i then
if j0-i0>i-k then
begin
i0:=k;
j0:=i;
end
end;
writeln('gain obtenu avec un achat a ',i0,' et une vente a ',j0,'.');
gain1:=gain_tmp;
end;
procedure gain1j( t: table; j:integer;
var i_opt,j_opt : integer;
var gain_opt : real);
begin
var i,i0,j0,k : integer;
var gain_tmp, gain_i : real;
begin
k:=j;gain_tmp:=0;
for i:=j to long_tab do
begin
if t[i]<= t[k] then
begin
k:=i;
gain_i:=0
end
else
gain_i:=t[i]-t[k];
if gain_tmp<gain_i then
begin
gain_tmp:=gain_i;
j0:=i;
i0:=k;
end
else if gain_tmp=gain_i then
if j0-i0>i-k then
begin
i0:=k;
j0:=i;
end
end;
j_opt:=j0;
i_opt:=i0;
gain_opt:=gain_tmp;
end;
end;
function gain2(t : table):real;
var min,gain_max_j, gain_tmp_gauche, gain_tmp_droite, gain_tmp : real;
var j,i, i_tmp, iprime, jprime,i0,j0,i1,j1 : integer;
begin
gain_tmp:=0;
i0:=1;j0:=1;i1:=1;j1:=1;
for j:=1 to long_tab do
begin
gain_max_j:=0;
min:=t[1];
i_tmp:=1;
for i:=1 to j do
begin
if t[i]<min then
begin
min:=t[i];
i_tmp:=i
end
end;
gain_tmp_gauche := t[j]-min;
gain1j(t,j,iprime,jprime,gain_tmp_droite);
gain_max_j:=gain_tmp_gauche+gain_tmp_droite;
if gain_max_j>gain_tmp then
begin
gain_tmp:=gain_max_j;
i0:=i_tmp;
j0:=j;
i1:=iprime;
j1:=jprime;
end;
end;
gain2:=gain_tmp;
writeln('transactions ',i0, ' et ', j0, ', puis ',i1, ' et ', j1, ' .');
end;
var A,B,B2,C,D,E:table;
var i : integer;
var tmp : real;
begin
readln;
A[1]:=70;
for i:=2 to long_tab do
begin
if i mod 2 = 0 then A[i]:=A[i-1]-2 else A[i]:=A[i-1]+1
end;
visu(A);
readln;
B[1]:=120;
for i:=2 to 9 do
begin
B[i]:=B[i-1]+2
end;
for i:=10 to long_tab do
begin
B[i]:=B[i-1]-1
end;
visu(B);
readln;
B2[1]:=120;
for i:=2 to 9 do
begin
B2[i]:=B2[i-1]+2
end;
for i:=10 to 20 do
begin
B2[i]:=B2[i-1]-1
end;
for i:=21 to 40 do
begin
B2[i]:=B2[i-1]+3
end;
for i:=41 to long_tab do
begin
B2[i]:=B2[i-1]-2
end;
visu(B2);
readln;
C[1]:=100;
for i:=2 to long_tab do
begin
tmp:=(8+random(4))/10;
C[i]:=max(tmp*C[i-1],0)
end;
visu(C);
readln;
D[1]:=100;
for i:=2 to long_tab do
begin
tmp:=(92+random(20))/100;
D[i]:=max(tmp*D[i-1],0)
end;
visu(D);
readln;
E[1]:=100;
for i:=2 to long_tab do
begin
tmp:=(88+random(20))/100;
E[i]:=max(tmp*E[i-1],0)
end;
visu(E);
readln;
writeln('amplitudes :');
writeln('A : ',amplitude(A):3:2);
writeln('B : ',amplitude(B):3:2);
writeln('B2 : ',amplitude(B2):3:2);
writeln('C : ',amplitude(C):3:2);
writeln('D : ',amplitude(D):3:2);
writeln('E : ',amplitude(E):3:2);
readln;
writeln('gains :');
writeln('A : ',gain(A):3:2);
writeln('B : ',gain(B):3:2);
writeln('B2 : ',gain(B2):3:2);
writeln('C : ',gain(C):3:2);
writeln('D : ',gain(D):3:2);
writeln('E : ',gain(E):3:2);
readln;
writeln('gains : (bis)');
writeln('A : ',gain1(A):3:2);
writeln('B : ',gain1(B):3:2);
writeln('B2 : ',gain(B2):3:2);
writeln('C : ',gain1(C):3:2);
writeln('D : ',gain1(D):3:2);
writeln('E : ',gain1(E):3:2);
readln;
writeln('gains avec 2 transactions :');
writeln('A : ',gain2(A):3:2,' ');
writeln('B : ',gain2(B):3:2,' ');
writeln('B2 : ',gain2(B2):3:2,' ');
writeln('C : ',gain2(C):3:2,' ');
writeln('D : ',gain2(D):3:2,' ');
writeln('E : ',gain2(E):3:2,' ');
readln;
end.