最近我有一个任务,但我卡在这里。问题是等待时间'。它是生成但它以0值开始。我只知道如何做累积频率,但它不是从0开始。我真的需要帮助。 提前致谢。 :) 〜图1是我的任务。 (我必须像这样做桌子)
def custom_loss(y_true, y_pred)
# y_true has the payoffs in the second row
payoffs = y_true[:, 1]
payoffs = K.expand_dims(payoffs, 1)
y_true = y_true[:, 0]
y_true = K.expand_dims(y_true, 1))
loss = K.binary_crossentropy(y_true, y_pred)
return loss
}
答案 0 :(得分:1)
您正在尝试计算[k]处的累计等待时间,这是处理[k-1] +处理[k-1]处的CPU突发的等待时间,其中k> 0。 0.
以下是代码:
public class SimpleArray {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numberOfProcesses;
System.out.print("Processes: ");
numberOfProcesses = input.nextInt();
String[] process = new String[numberOfProcesses];
int[] CPUburst = new int[numberOfProcesses];
int[] priority = new int[numberOfProcesses];
int[] waitingTime = new int[numberOfProcesses];
for (int i = 0; i < numberOfProcesses; i++) {
System.out.print("Process>> ");
process[i] = input.next();
System.out.print("Enter the CPU Burst>> ");
CPUburst[i] = input.nextInt();
System.out.print("Enter the Priority>> ");
priority[i] = input.nextInt();
}
System.out.println(" | Process | CPU Burst | Priority | Waiting Time | ");
int temp = 0;
for (int k = 0; k < numberOfProcesses; k++) {
if (k > 0)
waitingTime[k] = waitingTime[k-1] + CPUburst[k-1];
System.out.println(" | " + process[k] + " | " + CPUburst[k] + " | " + priority[k] + " | " + waitingTime[k] + " | ");
}
double averageWaitTime = 0;
if (numberOfProcesses > 0)
averageWaitTime = (double)waitingTime[numberOfProcesses-1] / (numberOfProcesses);
System.out.println("Average wait time = "+averageWaitTime);
}
}
产生结果:
| Process | CPU Burst | Priority | Waiting Time |
| A | 7 | 3 | 0 |
| B | 2 | 5 | 7 |
| C | 3 | 1 | 9 |
| D | 6 | 4 | 12 |
| E | 4 | 2 | 18 |
Average wait time = 3.6
编辑:在运行算法之前按CPU Burst排序
public class SimpleArray {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numberOfProcesses;
System.out.print("Processes: ");
numberOfProcesses = input.nextInt();
String[] process = new String[numberOfProcesses];
int[] CPUburst = new int[numberOfProcesses];
int[] priority = new int[numberOfProcesses];
int[] waitingTime = new int[numberOfProcesses];
for (int i = 0; i < numberOfProcesses; i++) {
System.out.print("Process>> ");
process[i] = input.next();
System.out.print("Enter the CPU Burst>> ");
CPUburst[i] = input.nextInt();
System.out.print("Enter the Priority>> ");
priority[i] = input.nextInt();
}
int tempo;
String s;
for (int i = 0; i < numberOfProcesses; i++) {
for (int j = i + 1; j < numberOfProcesses; j++) {
if (CPUburst[i] > CPUburst[j]) {
tempo = CPUburst[i];
CPUburst[i] = CPUburst[j];
CPUburst[j] = tempo;
tempo = priority[i];
priority[i] = priority[j];
priority[j] = tempo;
s = process[i];
process[i] = process[j];
process[j] = s;
}
}
}
System.out.println(" | Process | CPU Burst | Priority | Waiting Time | ");
int temp = 0;
for (int k = 0; k < numberOfProcesses; k++) {
if (k > 0)
waitingTime[k] = waitingTime[k - 1] + CPUburst[k - 1];
System.out.println(" | " + process[k] + " | " + CPUburst[k] + " | " + priority[k]
+ " | " + waitingTime[k] + " | ");
}
double averageWaitTime = 0;
if (numberOfProcesses > 0)
averageWaitTime = (double) waitingTime[numberOfProcesses - 1] / (numberOfProcesses);
System.out.println("Average wait time = " + averageWaitTime);
}
}
现在生成:
| Process | CPU Burst | Priority | Waiting Time |
| B | 2 | 5 | 0 |
| C | 3 | 1 | 2 |
| E | 4 | 2 | 5 |
| D | 6 | 4 | 9 |
| A | 7 | 3 | 15 |
Average wait time = 3.0
编辑2:显示如何确保输入正确
int numberOfProcesses = 0;
boolean success = true;
do {
try {
System.out.print("Processes: ");
String s = input.nextLine();
numberOfProcesses = Integer.parseInt(s);
success = true;
} catch (NumberFormatException e) {
success = false;
System.out.println("Wrong Input. Please put integer number.");
}
} while (!success);