Pro_2.pas 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. {理想收入问题,包括算法2-1,2-2,2-3和2-4}
  2. {$M 65520,0,655360}
  3. const
  4. Fn ='2.in';
  5. MaxM =5000;{M的最大值}
  6. var
  7. M :Integer;{天数}
  8. V :array[0..MaxM] of Extended;{股票价格}
  9. Result :Extended;{答案}
  10. Time :Longint;
  11. procedure Create;{为了方便测试,生成随机数据}
  12. var
  13. i :Integer;
  14. f :Text;
  15. begin
  16. Randomize;
  17. Writeln('Create Random Data ');
  18. Write('Input M (1<=M<=',MaxM,') : ');Readln(M);
  19. Assign(f,Fn);ReWrite(f);
  20. Writeln(f,M);
  21. for i:=1 to M do Writeln(f,Random(M)+1.0*M*M*M*M:0:0);
  22. Close(f)
  23. end;
  24. procedure GetInfo;{读入数据}
  25. var
  26. i :Integer;
  27. f :Text;
  28. begin
  29. Assign(f,Fn);Reset(f);
  30. Readln(f,M);
  31. for i:=1 to M do Readln(f,V[i]);
  32. Close(f)
  33. end;
  34. function Max(x,y:Extended):Extended;
  35. begin
  36. if x>y then Max:=x else Max:=y
  37. end;
  38. procedure Main1;{算法2-1}
  39. var
  40. F :array[0..MaxM] of Extended;
  41. i,j,k :Integer;
  42. begin
  43. F[0]:=1;V[0]:=1;
  44. Result:=1;
  45. for i:=1 to M do begin
  46. F[i]:=0;
  47. for j:=0 to i-1 do
  48. for k:=j to i-1 do
  49. F[i]:=Max(F[i],F[j]/V[k]*V[i]);
  50. if F[i]>Result then Result:=F[i]
  51. end
  52. end;
  53. procedure Main2;{算法2-2}
  54. var
  55. F :array[0..MaxM] of Extended;
  56. i,j :Integer;
  57. MaxFV :Extended;
  58. begin
  59. F[0]:=1;V[0]:=1;MaxFV:=1;
  60. Result:=1;
  61. for i:=1 to M do begin
  62. for j:=0 to i-1 do
  63. MaxFV:=Max(MaxFV,F[j]/V[i-1]);
  64. F[i]:=MaxFV*V[i];
  65. if F[i]>Result then Result:=F[i]
  66. end
  67. end;
  68. procedure Main3;{算法2-3}
  69. var
  70. i :Integer;
  71. F,MaxF,MaxFV :Extended;
  72. begin
  73. F:=1;MaxF:=1;MaxFV:=1;V[0]:=1;
  74. for i:=1 to M do begin
  75. if F>MaxF then MaxF:=F;
  76. if MaxF/V[i-1]>MaxFV then MaxFV:=MaxF/V[i-1];
  77. F:=MaxFV*V[i]
  78. end;
  79. Result:=Max(MaxF,F)
  80. end;
  81. procedure Main4;{算法2-4}
  82. var
  83. i :Integer;
  84. begin
  85. Result:=1;V[0]:=1;
  86. for i:=1 to M do
  87. if V[i]>V[i-1] then Result:=Result*V[i]/V[i-1];
  88. end;
  89. procedure InitTimer;
  90. begin
  91. Time:=MemL[$40:$6C];
  92. end;
  93. procedure DoneTimer;
  94. begin
  95. Writeln(' Time Used = ',MemL[$40:$6C]-Time)
  96. end;
  97. begin
  98. Create;
  99. GetInfo;
  100. Writeln('Algorithm 1 : ');
  101. InitTimer;
  102. Main1;
  103. DoneTimer;
  104. Writeln(' Result = ',Result:0:1);
  105. Writeln('Algorithm 2 : ');
  106. InitTimer;
  107. Main2;
  108. DoneTimer;
  109. Writeln(' Result = ',Result:0:1);
  110. Writeln('Algorithm 3 : ');
  111. InitTimer;
  112. Main3;
  113. DoneTimer;
  114. Writeln(' Result = ',Result:0:1);
  115. Writeln('Algorithm 4 : ');
  116. InitTimer;
  117. Main4;
  118. DoneTimer;
  119. Writeln(' Result = ',Result:0:1);
  120. end.