首先適應演算法,最優適應演算法,最壞適應演算法
package oj.test;
import java.util.*;
import java.util.regex.Pattern;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Area[] area = new Area[5];
area[0] = new Area(0, 20);
area[1] = new Area(1, 30);
area[2] = new Area(2, 40);
area[3] = new Area(3, 50);
area[4] = new Area(4, 150);
Memory[] memory = new Memory[4];
memory[0] = new Memory(0, 100);
memory[1] = new Memory(1, 30);
memory[2] = new Memory(2, 50);
memory[3] = new Memory(3, 40);
//FirstAdapt(memory, area);
BestAdapt(memory, area);
//BadAdapt(memory,area);
}
//首先適應演算法
public static void FirstAdapt(Memory[] memory, Area[] area) {
int k;
for (int i = 0; i < memory.length; i++) {
k = area.length;
for (int j = 0; j < area.length; j++) {
if ((memory[i].getValue()) <= (area[j].getValue())) {
area[j].setValue(area[j].getValue() - memory[i].getValue());
System.out.println(memory[i] + "--->" + area[j]);
break;
}else
k--;
if (k < 1) {
System.out.println(memory[i] + " Failed !");
}
}
}
}
//最優適應演算法
public static void BestAdapt(Memory[] memory, Area[] area) {
int i, j;
for (i = 0; i < memory.length; i++) {
boolean flag = true;
int min = 0;
for (j = 0; j < area.length; j++) {
if (flag) {
if (memory[i].getValue() <= area[j].getValue()) {
min = area[j].getValue();
flag = false;
}
} else if ((memory[i].getValue() <= area[j].getValue())&& (min >= area[j].getValue())) {
min = area[j].getValue();
}
}
if (min == 0) {
System.out.println(memory[i] + " Failed !");
} else {
for (j = 0; j < area.length; j++) {
if (area[j].getValue() == min) {
System.out.println(memory[i] + "--->" + area[j]);
area[j].setValue(min - memory[i].getValue());
break;
}
}
}
}
}
//最壞適應演算法
public static void BadAdapt(Memory[] memory, Area[] area) {
int i, j;
for (i = 0; i < memory.length; i++) {
boolean flag = true;
int max = 0;
for (j = 0; j < area.length; j++) {
if (flag) {
if (memory[i].getValue() <= area[j].getValue()) {
max = area[j].getValue();
flag = false;
}
} else if ((memory[i].getValue() <= area[j].getValue()) && (max <= area[j].getValue())) {
max = area[j].getValue();
}
}
if (max == 0) {
System.out.println(memory[i] + " Failed !");
} else {
for (j = 0; j < area.length; j++) {
if (area[j].getValue() == max) {
System.out.println(memory[i] + "--->" + area[j]);
area[j].setValue(max - memory[i].getValue());
break;
}
}
}
}
}
private static void sop(Object o) {
System.out.println(o);
}
}
//定義一個記憶體作業區
class Area {
private int id;
private int value;
Area(int id) {
this.id = id;
}
Area(int id, int value) {
this.id = id;
this.value = value;
}
public String toString() {
return "Area:" + id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
//定義一個記憶體作業
class Memory {
Area[] area = new Area[5];
Memory[] memory = new Memory[4];
private int id;
private int value;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
Memory(int id) {
this.id = id;
}
Memory(int id, int value) {
this.id = id;
this.value = value;
}
public String toString() {
return "memory:" + id;
}
}