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.

66 lines
2.5 KiB

import java.util.Scanner;
import java.util.Queue;
import java.util.LinkedList;
public class BankQueueSimulation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入顾客总人数N: ");
int N = scanner.nextInt();
Queue<Customer> customerQueue = new LinkedList<>();
System.out.println("请输入每位顾客的到达时间T和处理时间P按到达时间先后顺序输入:");
for (int i = 0; i < N+1; i++) {
int arrivalTime = scanner.nextInt();
int processTime = scanner.nextInt();
customerQueue.add(new Customer(arrivalTime, processTime));
}
System.out.print("请输入每位顾客事务被处理的最长时间MaxProc分钟: ");
int MaxProc = scanner.nextInt();
System.out.print("请输入银行最多可开设的营业窗口MaxWindow: ");
int MaxWindow = scanner.nextInt();
double averageWaitTime = simulateQueue(customerQueue, MaxProc, MaxWindow);
System.out.printf("所有顾客的平均等待时间为: %.2f 分钟%n", averageWaitTime);
}
private static double simulateQueue(Queue<Customer> customerQueue, int MaxProc, int MaxWindow) {
int totalWaitTime = 0;
int numCustomers = customerQueue.size();
int[] windowEndTime = new int[MaxWindow];
while (!customerQueue.isEmpty()) {
Customer currentCustomer = customerQueue.poll();
int minEndTime = Integer.MAX_VALUE;
int chosenWindow = -1;
// 找到最早可用的窗口
for (int i = 0; i < MaxWindow; i++) {
if (windowEndTime[i] < minEndTime) {
minEndTime = windowEndTime[i];
chosenWindow = i;
}
}
int waitTime = Math.max(0, minEndTime - currentCustomer.arrivalTime);
totalWaitTime += waitTime;
// 更新该窗口的结束时间
windowEndTime[chosenWindow] = currentCustomer.arrivalTime + waitTime + Math.min(currentCustomer.processTime, MaxProc);
}
return (double) totalWaitTime / numCustomers;
}
static class Customer {
int arrivalTime;
int processTime;
public Customer(int arrivalTime, int processTime) {
this.arrivalTime = arrivalTime;
this.processTime = processTime;
}
}
}