You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.4 KiB
75 lines
2.4 KiB
import java.util.*;
|
|
|
|
public class BankQueue {
|
|
|
|
static class Customer {
|
|
int arrivalTime;
|
|
int processTime;
|
|
|
|
public Customer(int arrivalTime, int processTime) {
|
|
this.arrivalTime = arrivalTime;
|
|
this.processTime = processTime;
|
|
}
|
|
}
|
|
|
|
public static double calculateAverageWaitTime(List<Customer> customers, int maxProc, int maxWindow) {
|
|
Queue<Integer>[] windows = new LinkedList[maxWindow];
|
|
for (int i = 0; i < maxWindow; i++) {
|
|
windows[i] = new LinkedList<>();
|
|
}
|
|
|
|
int totalWaitTime = 0;
|
|
int numCustomersServed = 0;
|
|
|
|
for (Customer customer : customers) {
|
|
// 寻找最早空闲的窗口
|
|
int minFinishTime = Integer.MAX_VALUE;
|
|
int chosenWindow = -1;
|
|
|
|
for (int i = 0; i < maxWindow; i++) {
|
|
if (windows[i].isEmpty()) {
|
|
chosenWindow = i;
|
|
break;
|
|
}
|
|
int finishTime = windows[i].peek();
|
|
if (finishTime < minFinishTime) {
|
|
minFinishTime = finishTime;
|
|
chosenWindow = i;
|
|
}
|
|
}
|
|
|
|
// 计算等待时间
|
|
int waitTime = Math.max(0, minFinishTime - customer.arrivalTime);
|
|
totalWaitTime += waitTime;
|
|
|
|
// 更新服务结束时间
|
|
int serviceEndTime = customer.arrivalTime + waitTime + customer.processTime;
|
|
if (serviceEndTime - customer.arrivalTime <= maxProc) {
|
|
windows[chosenWindow].offer(serviceEndTime);
|
|
numCustomersServed++;
|
|
}
|
|
}
|
|
|
|
if (numCustomersServed == 0) {
|
|
return 0; // 没有顾客被服务
|
|
}
|
|
|
|
return (double) totalWaitTime / numCustomersServed;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
// 假设的输入数据
|
|
List<Customer> customers = Arrays.asList(
|
|
new Customer(0, 3),
|
|
new Customer(2, 3),
|
|
new Customer(5, 4),
|
|
new Customer(6, 2)
|
|
);
|
|
|
|
int maxProc = 10;
|
|
int maxWindow = 2;
|
|
|
|
double avgWaitTime = calculateAverageWaitTime(customers, maxProc, maxWindow);
|
|
System.out.println("平均等待时间: " + avgWaitTime);
|
|
}
|
|
} |