Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- /*
- * author : yufieko
- */
- public class BestFit {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int size,kosong,pakai,proses,t_ksg = 0,t_pki = 0;
- boolean error;
- System.out.println(" =================== Best Fit ==================");
- System.out.print(" Ukuran memori : "); size = input.nextInt();
- System.out.print(" Jumlah partisi memori kosong : "); kosong = input.nextInt();
- System.out.print(" Jumlah partisi memori terpakai : "); pakai = input.nextInt();
- System.out.println(" -----------------------------------------------");
- System.out.println("\n Partisi Memori Kosong : input (awal akhir)");
- System.out.println(" -----------------------------------------------");
- int tksg[] = new int[kosong];
- int ksg[][] = new int[kosong][2];
- for(int i=0; i<kosong; i++){
- int j = 0;
- System.out.print(" Partisi-" + (i+1) + " : ");
- ksg[i][j] = input.nextInt();
- ksg[i][j+1] = input.nextInt();
- if(ksg[i][j] > size){
- i -= 1;
- System.out.println(" ! Batas awal melebihi ukuran");
- }else if(ksg[i][j+1] > size){
- i -= 1;
- System.out.println(" ! Batas akhir melebihi ukuran");
- }else{
- tksg[i] = ksg[i][j+1]-ksg[i][j];
- t_ksg = t_ksg + (ksg[i][j+1]-ksg[i][j]);
- }
- }
- System.out.println("\n Partisi Memori Terpakai : input (awal akhir)");
- System.out.println(" -----------------------------------------------");
- int tpki[] = new int[pakai];
- int pki[][] = new int[pakai][2];
- for(int i=0; i<pakai; i++){
- int j = 0;
- System.out.print(" Partisi-" + (i+1) + " : ");
- pki[i][j] = input.nextInt();
- pki[i][j+1] = input.nextInt();
- if(pki[i][j] > size){
- i -= 1;
- System.out.println(" ! Batas awal melebihi ukuran");
- }else if(pki[i][j+1] > size){
- i -= 1;
- System.out.println(" ! Batas akhir melebihi ukuran");
- }else{
- tpki[i] = pki[i][j+1]-pki[i][j];
- t_pki = t_pki + (pki[i][j+1]-pki[i][j]);
- }
- }
- System.out.println("\n Proses");
- System.out.println(" -----------------------------------------------");
- boolean penuh = false;
- int i = 0;
- while(!penuh){
- int j = 0;
- System.out.println("\n Ukuran asli : " + size);
- System.out.println(" Total kosong : " + t_ksg);
- System.out.println(" Total terpakai : " + t_pki + "\n");
- for(int y=0; y<kosong; y++)
- System.out.println(" Partisi-" + (y+1) + " -> " + tksg[y]);
- System.out.println(" -----------------------------------------------");
- System.out.print(" > Proses-" + (i+1) + " (input ukuran) : ");
- proses = input.nextInt();
- boolean cek = false;
- int l = 0;
- while(l<kosong){
- int temp = 0;
- int b[] = new int[kosong];
- System.arraycopy(tksg,0,b,0,tksg.length);
- for(int k=0;k<kosong;k++){
- for(int m=0 ; m<kosong-1; m++){
- if(b[m] > b[m+1]){
- temp = b[m];
- b[m] = b[m+1];
- b[m+1] = temp;
- }
- }
- }
- for(int k=0; k<kosong ; k++){
- if(b[k] >= proses){l = k; break;}
- }
- for(int k=0; k<kosong; k++){
- if(tksg[k] == b[l]){l = k; break;}
- }
- if(tksg[l] >= proses){
- System.out.println(" # Proses-" + (i+1) + " --> Partisi-" + (l+1) + " (kosong : " + tksg[l] + ")");
- tksg[l] -= proses;
- t_ksg -= proses;
- t_pki += proses;
- ksg[l][j] += proses;
- System.out.println(" - Sisa Partisi-" + (l+1) + " = " + tksg[l]);
- cek = true;
- break;
- }else{
- if(tksg[l] == 0) System.out.println(" ! Partisi-" + (l+1) + " penuh");
- else System.out.println(" ! Partisi-" + (l+1) + " tidak cukup");
- cek = false;
- break;
- }
- }
- if(!cek){
- System.out.println(" ! Permintaan Proses-" + (i+1) + " tidak bisa dipenuhi");
- i -= 1;
- }
- if(t_ksg == 0){
- System.out.println(" ! Memori penuh");
- System.out.println("\n Ukuran asli : " + size);
- System.out.println(" Total kosong : " + t_ksg);
- System.out.println(" Total terpakai : " + t_pki);
- penuh = true;
- }
- i++;
- }
- }
- }
- // lpuarmy.blogspot.com
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement