diff --git a/银行问题1.txt b/银行问题1.txt new file mode 100644 index 0000000..07d6151 --- /dev/null +++ b/银行问题1.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