Advertisement
ProToTN

Optimisation: Number of ships needed to transport X crates.

Jan 21st, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.85 KB | None | 0 0
  1. {You have a certain number of crates that weight differently, how many ships do you need to transport them all at the same time?}
  2.  
  3. uses wincrt;
  4. Type Tab=array[1..100] of integer;
  5. Var NC,BK:integer; TPC:tab;
  6.  
  7.  
  8.  
  9. Procedure remplissageTPC(var TPC:Tab;NC,BK:Integer);
  10. Var i:byte;
  11. Begin
  12.   For i:=1 to NC do
  13.   Repeat
  14.     Writeln('Write down the weight of crate number ',i);
  15.     Readln(TPC[i]);
  16.   Until (TPC[i]>0) and (TPC[i]<BK);
  17. End;
  18.  
  19.  
  20.  
  21. Procedure triTPC(var TPC:tab;NC:integer);
  22. Var verif:boolean;i:byte;x:integer;
  23. Begin
  24.   Repeat
  25.     verif:=true;
  26.     For i:=1 to NC-1 do
  27.     Begin
  28.       writeln(i,' ',nc);
  29.       if TPC[i]<TPC[i+1] then
  30.       Begin
  31.         x:=TPC[i];
  32.         TPC[i]:=TPC[i+1];
  33.         TPC[i+1]:=x;
  34.         verif:=false;
  35.       End;
  36.     End;
  37.   Until verif=true;
  38. End;
  39.  
  40.  
  41.  
  42. Procedure Saisie(var NC,BK:integer);
  43. Begin
  44.   Repeat
  45.     Writeln('Number of crates');
  46.     readln(NC);
  47.   Until NC>0;
  48.   Repeat
  49.     Writeln('Maximal weight per boat');
  50.     Readln(BK);
  51.   Until BK>0;
  52.   remplissageTPC(TPC,NC,BK);
  53.   triTPC(TPC,NC);
  54.   writeln('FintriTPC');
  55. End;
  56.  
  57.  
  58.  
  59. Function Verif(TPC:tab;NC:integer):boolean;
  60. var i:byte;
  61. Begin
  62.   i:=1;
  63.   While (TPC[i]=0) and (i<=NC) do
  64.   i:=i+1;
  65.   Verif:=(i>NC);
  66. End;
  67.  
  68.  
  69.  
  70. Procedure Affichage( NC,BK:integer;var TPC:tab);
  71. var i:byte; S,SP,NB:integer;
  72. Begin
  73.   S:=TPC[1];
  74.   TPC[1]:=0;
  75.   i:=2;
  76.   NB:=0;
  77.   Repeat
  78.     Repeat
  79.       SP:=S;
  80.       S:=S+TPC[i];
  81.       if S<=BK then
  82.       Begin
  83.         TPC[i]:=0;
  84.         i:=i+1;
  85.       End
  86.       else
  87.       Begin
  88.         S:=SP;
  89.         i:=i+1;
  90.       End;
  91.     Until (S=BK) or (i>NC);
  92.     If (S=BK) or (i>NC) then NB:=NB+1;
  93.     S:=0;
  94.     i:=2;
  95.   Until Verif(TPC,NC);
  96.   Writeln('Number of ships:', NB);
  97. End;
  98.  
  99.  
  100. Begin
  101.   Saisie(NC,BK);
  102.   Affichage(NC,BK,TPC);
  103. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement