From 760025b3c8c8a4331fb3734e6a6ca16ee23f4f4d Mon Sep 17 00:00:00 2001 From: pwgliku62 <2867008117@qq.com> Date: Sat, 1 Jun 2024 16:47:23 +0800 Subject: [PATCH] ADD file via upload --- 银行排队问题.txt | 75 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 银行排队问题.txt diff --git a/银行排队问题.txt b/银行排队问题.txt new file mode 100644 index 0000000..07d6151 --- /dev/null +++ b/银行排队问题.txt @@ -0,0 +1,75 @@ +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 customers, int maxProc, int maxWindow) { + Queue[] 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 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); + } +} \ No newline at end of file