diff --git a/银行问题1.txt b/银行问题1.txt new file mode 100644 index 0000000..a92b04e --- /dev/null +++ b/银行问题1.txt @@ -0,0 +1,65 @@ +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 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 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; + } + } +}