队列模拟来计算客户等待时间

时间:2014-11-19 05:25:44

标签: java data-structures

我在过去10-12小时内一直在解决这个问题,并且想知道你们是否可以帮助我调试/指出我正确的方向。该程序的目的是模拟快餐店队列,我试图使用以下方法完成:

PriorityQueue(FIFO)数据结构

我已经与同事,校内辅导服务,教授和给定的课程教材进行了咨询:" Java如何编程:Deitel&的Deitel"无济于事。

提供的问题的伪代码如下(我不想让你为我做这件事):

BigBurger Inc. wants to see if having a single person at the counter both to take orders and to serve them is feasible. At each BigBurger, customers will arrive and get in line. When they get to the head of the line they will place their order, which will be assembled and served to them. Then they will leave the BigBurger and the next person in line will be able to order.
We need to know how long a customer may be forced to wait before he or she can place an order. Given a script that lists each customer for a typical day, we want to calculate the maximum customer waiting time. Each customer in the script is characterized by an arrival time (measured in minutes after the store opened) and a service duration (the number of minutes between ordering and getting the food).
Create a class BigBurger that contains method maxWait that is given a int[] arrival and a int[] service describing all the customers and returns the maximum time spent by a customer between arriving and placing the order. Corresponding elements of arrival and service refer to the same customer, and they are given in the order in which they arrive at the store (arrival is in non-descending order).
If multiple customers arrive at the same time they will all join the line at the same time, with the ones listed earlier ahead 
of ones appearing later in the list.
Definition
    
Class:
BigBurger
Method:
maxWait
Parameters:
int[], int[]
Returns:
int
Method signature:
int maxWait(int[] arrival, int[] service)
(be sure your method is public)
    
Constraints-
arrival will contain between 1 and 50 elements inclusive-
service will contain the same number of elements as arrival-
the elements of arrival will be in non-decreasing order-
each element of arrival will be between 1 and 720 inclusive-
each element of service will be between 1 and 15 inclusive

Examples    
{3,3,9}
{2,15,14}
Returns: 11
Two customers arrive at time 3. The first one waits 0 time, orders, and is served after 2 minutes, leaving at time 5. The second one then orders and is served at time 20. Meanwhile a customer arrives at time 9 and waits until the second customer leaves. This last customer then orders at time 20, and is served and leaves at time 20+14 = 34. The first customer waited 0 minutes, the second waited 2 minutes (from time 3 to time 5), and the last customer waited 11 minutes (from time 9 to time 20).
    

我已经在网上进行了研究,通常使用系统纳米时间或使用随机方法计算到达时间,但在这种情况下,示例中已经提供了到达时间和服务时间,我必须计算每个客户的总等待时间。因为我不熟悉编码,请指导我。

我遇到的问题:

当我在方法maxWait(int [],int [])中调用return maxWaitTime时,无法为客户打印maxWaitTime

这是我的代码:

import java.util.*;


public class QueueProgram 
{
    static int[] arrival = {3,3,9};
    static int[] service = {2,15,14};

    int waitTime;
    int finishTime;
    int serviceTime;
    static int index;


    Queue<Integer> Customers = new LinkedList<Integer>();


    public int maxWait(int[] arrival, int[] service)
    {
        //this.arrival = arrival;
        //this.service = service
        int maxWaitTime = 0;
        int[]finishTime = new int[arrival.length];

        for(int i=0; i<arrival.length;i++)
        {
            int startTime;
            index = i;
            if(index == 0)
            {
                 startTime = arrival[index];
                 System.out.println(startTime);
            }
            else
            {
                startTime = Math.max(arrival[i],finishTime[i-1]);
            }
            finishTime[i] = startTime + service[i];
            waitTime = finishTime[i] - service[i] - arrival[i];

            if(waitTime > maxWaitTime)
            {
                maxWaitTime = waitTime;
            }
        }
        return maxWaitTime;
    }

    public static void main(String[] args) 
    {
        QueueProgram q = new QueueProgram();
        q.maxWait(arrival, service);
    }

}

2 个答案:

答案 0 :(得分:1)

import java.util.*;


public class QueueProgram 
{
    static int[] arrival = {3,3,9};
    static int[] service = {2,15,14};

    int waitTime;
    int finishTime;
    int serviceTime;
    static int index;

    Queue<Integer> Customers = new LinkedList<Integer>();   

    public int maxWait(int[] arrival, int[] service)
    {
        //this.arrival = arrival;
        //this.service = service
        int maxWaitTime = 0;
        int[]finishTime = new int[arrival.length];

        for(int i=0; i<arrival.length;i++)
        {
            int startTime;
            index = i;
            if(index == 0)
            {
                 startTime = arrival[index];
                 //System.out.println(startTime);
            }
            else
            {
                startTime = Math.max(arrival[i],finishTime[i-1]);
            }
            finishTime[i] = startTime + service[i];
            waitTime = finishTime[i] - service[i] - arrival[i];

            if(waitTime > maxWaitTime)
            {
                maxWaitTime = waitTime;
            }
        }
        return maxWaitTime;
    }

    public static void main(String[] args) 
    {
        QueueProgram q = new QueueProgram();
        q.maxWait(arrival, service);
        System.out.println("Maximum wait time is: " + q.maxWait(arrival, service));
    }

}

答案 1 :(得分:0)

变量index是多余的,i已经表示数组索引。其次,waitTime可以计算为finshTime[i-1]-arrival[i],无需计算startTime。较少的操作更好的空间和时间复杂性。

试试这个:

   for(int i=0; i<arrival.length;i++)
    {       
      if(i != 0) { 
        waitTime = finishTime[i-1] - arrival[i];
        if(waitTime > maxWaitTime)
        { maxWaitTime = waitTime;}
      }
      finishTime[i] = arrival[i] + service[i];         
    }