Advertisement
lpuarmy

Best Fit | SO

Jun 16th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.07 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /*
  4.  * author : yufieko
  5.  */
  6.  
  7. public class BestFit {
  8.     public static void main(String[] args) {
  9.         Scanner input = new Scanner(System.in);
  10.         int size,kosong,pakai,proses,t_ksg = 0,t_pki = 0;
  11.         boolean error;
  12.  
  13.         System.out.println(" =================== Best Fit ==================");
  14.         System.out.print(" Ukuran memori                  : "); size = input.nextInt();
  15.         System.out.print(" Jumlah partisi memori kosong   : "); kosong = input.nextInt();
  16.         System.out.print(" Jumlah partisi memori terpakai : "); pakai = input.nextInt();
  17.         System.out.println(" -----------------------------------------------");
  18.  
  19.         System.out.println("\n Partisi Memori Kosong : input (awal akhir)");
  20.         System.out.println(" -----------------------------------------------");
  21.        
  22.         int tksg[] = new int[kosong];
  23.         int ksg[][] = new int[kosong][2];
  24.         for(int i=0; i<kosong; i++){
  25.             int j = 0;
  26.  
  27.             System.out.print(" Partisi-" + (i+1) + " : ");
  28.             ksg[i][j] = input.nextInt();
  29.             ksg[i][j+1] = input.nextInt();
  30.            
  31.             if(ksg[i][j] > size){
  32.                 i -= 1;
  33.                 System.out.println(" ! Batas awal melebihi ukuran");
  34.             }else if(ksg[i][j+1] > size){
  35.                 i -= 1;
  36.                 System.out.println(" ! Batas akhir melebihi ukuran");
  37.             }else{
  38.                 tksg[i] = ksg[i][j+1]-ksg[i][j];
  39.                 t_ksg = t_ksg + (ksg[i][j+1]-ksg[i][j]);
  40.             }
  41.         }
  42.        
  43.         System.out.println("\n Partisi Memori Terpakai : input (awal akhir)");
  44.         System.out.println(" -----------------------------------------------");
  45.  
  46.         int tpki[] = new int[pakai];
  47.         int pki[][] = new int[pakai][2];
  48.         for(int i=0; i<pakai; i++){
  49.             int j = 0;
  50.  
  51.             System.out.print(" Partisi-" + (i+1) + " : ");
  52.             pki[i][j] = input.nextInt();
  53.             pki[i][j+1] = input.nextInt();
  54.  
  55.             if(pki[i][j] > size){
  56.                 i -= 1;
  57.                 System.out.println(" ! Batas awal melebihi ukuran");
  58.             }else if(pki[i][j+1] > size){
  59.                 i -= 1;
  60.                 System.out.println(" ! Batas akhir melebihi ukuran");
  61.             }else{
  62.                 tpki[i] = pki[i][j+1]-pki[i][j];
  63.                 t_pki = t_pki + (pki[i][j+1]-pki[i][j]);
  64.             }
  65.         }
  66.        
  67.         System.out.println("\n Proses");
  68.         System.out.println(" -----------------------------------------------");
  69.         boolean penuh = false;
  70.         int i = 0;
  71.         while(!penuh){
  72.             int j = 0;
  73.             System.out.println("\n Ukuran asli    : " + size);
  74.             System.out.println(" Total kosong   : " + t_ksg);
  75.             System.out.println(" Total terpakai : " + t_pki + "\n");
  76.  
  77.             for(int y=0; y<kosong; y++)
  78.                 System.out.println(" Partisi-" + (y+1) + " -> " + tksg[y]);
  79.  
  80.             System.out.println(" -----------------------------------------------");
  81.  
  82.             System.out.print(" > Proses-" + (i+1) + " (input ukuran) : ");
  83.             proses = input.nextInt();
  84.            
  85.             boolean cek = false;
  86.             int l = 0;
  87.             while(l<kosong){
  88.                 int temp = 0;
  89.                 int b[] = new int[kosong];
  90.                 System.arraycopy(tksg,0,b,0,tksg.length);
  91.            
  92.                 for(int k=0;k<kosong;k++){
  93.                     for(int m=0 ; m<kosong-1; m++){
  94.                         if(b[m] > b[m+1]){
  95.                             temp = b[m];
  96.                             b[m] = b[m+1];
  97.                             b[m+1] = temp;
  98.                         }
  99.                     }
  100.                 }
  101.  
  102.                 for(int k=0; k<kosong ; k++){
  103.                     if(b[k] >= proses){l = k; break;}
  104.                 }
  105.  
  106.                 for(int k=0; k<kosong; k++){
  107.                     if(tksg[k] == b[l]){l = k; break;}
  108.                 }
  109.  
  110.                 if(tksg[l] >= proses){
  111.                     System.out.println(" # Proses-" + (i+1) + " --> Partisi-" + (l+1) + " (kosong : " + tksg[l] + ")");
  112.                     tksg[l] -= proses;
  113.                     t_ksg -= proses;
  114.                     t_pki += proses;
  115.                     ksg[l][j] += proses;
  116.                     System.out.println(" - Sisa Partisi-" + (l+1) + " = " + tksg[l]);
  117.                     cek = true;
  118.                     break;
  119.                 }else{
  120.                     if(tksg[l] == 0) System.out.println(" ! Partisi-" + (l+1) + " penuh");
  121.                     else System.out.println(" ! Partisi-" + (l+1) + " tidak cukup");
  122.                     cek = false;
  123.                     break;
  124.                 }
  125.             }
  126.  
  127.             if(!cek){
  128.                 System.out.println(" ! Permintaan Proses-" + (i+1) + " tidak bisa dipenuhi");
  129.                 i -= 1;
  130.             }
  131.  
  132.             if(t_ksg == 0){
  133.                 System.out.println(" ! Memori penuh");
  134.                 System.out.println("\n Ukuran asli    : " + size);
  135.                 System.out.println(" Total kosong   : " + t_ksg);
  136.                 System.out.println(" Total terpakai : " + t_pki);
  137.                 penuh = true;
  138.             }
  139.             i++;
  140.         }
  141.     }
  142. }
  143. // lpuarmy.blogspot.com
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement