进程调度算法的模拟与实现

2025-01-07 02:31:27
推荐回答(1个)
回答1:

import java.util.Arrays;

public class SCAN {

public static void main(String[] args) {
SCAN s = new SCAN();

int[] a = { 55, 58, 39, 18, 90, 160, 150, 38, 184 };
int nowway = 100;

int[] high = s.high(nowway, a);// 高磁道
int hl = high.length;

int[] low = s.low(nowway, a);// 低磁道
int ll = low.length;

int[] all = new int[hl + ll];
for (int i = 0; i < hl; i++) {
all[i] = high[i];

}
for (int i = 0, j = 0; i < ll; i++, j++) {
all[hl + i] = low[j];
}

int[] movelength = new int[all.length];
for (int i = 0; i < all.length; i++) {
System.out.print(all[i] + " ");
int move = 0;
if (i == 0) {
move = all[0] - 100;
movelength[i] = move;
System.out.println("移动磁道数 " + move);
continue;

}
if (all[i] > all[i - 1]) {
move = all[i] - all[i - 1];
movelength[i] = move;

} else {
move = all[i - 1] - all[i];
movelength[i] = move;
}

System.out.println("移动磁道数 " + move);

}

int summovelentgth = 0;
for (int i = 0; i < movelength.length; i++) {
summovelentgth += movelength[i];

}
System.out.println("平均寻道长度:" + (double) summovelentgth
/ movelength.length);

}

public int[] low(int nowway, int[] a) {
int mincount = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] < nowway) {
mincount++; // 小于磁道数的
}
}

int[] a1 = new int[mincount];
int j = 0;

for (int i = 0; i < a.length; i++) {
if (a[i] < nowway) {

a1[j] = a[i]; // 小于磁道数的
j++;
}
}

Arrays.sort(a1);// 对小磁道升序排列
int k = a1.length;
int[] a3 = new int[k];
k--;
for (int i = 0; i < a1.length; i++, k--) { // 降序排列
a3[i] = a1[k];

}

return a3;
}

public int[] high(int nowway, int[] a) {

int maxcount = 0;

for (int i = 0; i < a.length; i++) {
if (a[i] > nowway) {

maxcount++; // 大于磁道数的
}
}

int[] a2 = new int[maxcount];
int j = 0;
int m = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] > nowway) {

a2[m] = a[i]; // 大于磁道数的
m++;
}
}

Arrays.sort(a2);// 对大磁道升序排列
return a2;
}

}