Thursday, October 10, 2013

Service counter

Consider a service counter. People waiting for service form a queue. At the start, the queue is empty and time is 0. The service time per person is 3 minutes. The input to the program consists of an integer N followed by a sequence of integers indicating the arrival time (in minutes) of the customers. These times are given as offset from the start of the simulation, so that an input of 44 means 44 minutes from the start of simulation.

The sequence of input numbers is terminated by the number -1. Your program must simulate the queue and print out the following:
  1. The number of customers waiting in the queue at time = N minutes from the start of simulation.
  2. The arrival times of the customers in the queue at that time, in increasing order.
Each integer must be separated by a space. Terminate your output with a newline character.

For doing the computation, you can assume that if the counter is expected to be vacant at time t, the first person in the queue will be scheduled for service, before the counting is done for time t. You must use the queue data structure to solve this problem.

Sample Input/Output:

INPUT :
9 0 2 5 6 6 8 10 -1
OUTPUT:
268

Solutions:

Using C Language ...

#include<stdio.h>
int main()
{
    int time = -1,te=0;
    int n,i=0,j, queue[100];

    while(1)
    {
        scanf("%d",&n);
        if(n == -1)
        break;
        if(time == -1)
        {
            time = n;
        }
        else if(n <= time)
        {
            if(te <= time)
            {
                if(te < n)
                te = n;
            }
            else
            {
                queue[i++] = n;
            }
            te = te+3;
        }
    }

    printf("%d",i);
    for(j=0;j<i;j++)
    {
        printf(" %d",queue[j]);
    }
    printf("\n");
    return 0;
}

No comments:

Post a Comment