1014 Waiting in Line 队列操作

目录

题目

输入样例

输出样例

提交结果截图

带详细注释的源代码


题目

题目链接:1014 Waiting in Line (PAT (Advanced Level) Practice)

输入样例

2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7

输出样例

08:07
08:06
08:10
17:00
Sorry

提交结果截图

带详细注释的源代码

代码参考了博文——PTA 1014 Waiting in Line (30分) 解题思路及满分代码

#include <iostream>
#include <queue>
#include <cstdio>
using namespace std;
#define open_time 8*60
#define close_time 17*60

struct Customer
{
	int process_time;
	int start_time;
	int finish_time;
}customer[1001];

int main()
{
	 int N,//the num of windows, <=20 
		 M,//max capacity of each line, <=10
		 K,//the num of customers, <=1000
         Q;//the num of queries, <=1000
    cin>>N>>M>>K>>Q;
	queue<Customer>Window[20];
	int queries[1001];
	for(int i = 1; i <= K; i++)
		cin>>customer[i].process_time;
	for(int i = 0; i < Q; i++)
		cin>>queries[i];
	for(int i = 1; i <= K; i++)
	{ 
		int window_num;
		if(i <= N*M)
		{
			window_num = (i-1)%N;//window num start from 0 while customer num start from 1
			if(i <= N)//their start time = open time
				customer[i].start_time = open_time;
			else
			{   //get the previous customer's infomation
				Customer pre_customer = Window[window_num].back();
				customer[i].start_time = pre_customer.finish_time;
			}
		}
		else
		{
			window_num = 0;//window num start from 0
			for(int j = 1; j < N; j++)//get the head customers's leaving time  
				if(Window[j].front().finish_time < Window[window_num].front().finish_time)
					window_num = j;
			Customer pre_customer = Window[window_num].back();get the head customer
			Window[window_num].pop();
			customer[i].start_time = pre_customer.finish_time;
		}
		customer[i].finish_time = customer[i].start_time + customer[i].process_time;
		Window[window_num].push(customer[i]);
	}
	//solve the queries
	for(int i = 0; i < Q; i++)
	{
		if(customer[queries[i]].start_time >= close_time)//if the bank is closed
			cout<<"Sorry"<<endl;
		else//output the finishing time
			printf("%02d:%02d\n", customer[queries[i]].finish_time/60, customer[queries[i]].finish_time%60);
	}
	return  0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值