您的位置:首页 > 其它

PAT(甲级)1014笔记--银行业务问题

2020-02-06 09:40 429 查看

PAT(甲级)1014笔记

1. 原题参照PAT官网

  • 这道题就是很有趣很难得亚子,花费了不少时间
  • 学习了这里的代码 【代码传送门】

2. 代码record

#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<queue>
#include<stack>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXSIZE 30
const int maxn = 22;/////////?????
const int maxk = 1000 + 10;///????

int N = 2, M = 0, K, Q,
customer_key[1000], customer_time[1000];
int nowTime = 0;
int CloseTime = (17 - 8) * 60;
struct customer {
int id;
int processtime;
int donetime;
customer() :donetime(-1) {}

}customers[maxk];

queue<customer> line[maxn], behindQueue;
int lastTime[maxn] = { 0 };
int main() {
scanf("%d%d%d%d", &N, &M, &K, &Q);
int capacity = N * M;
for (int i = 1; i <= K; i++)
{
scanf("%d", &customers[i].processtime);//输入每个人需要多长时间去办理业务
customers[i].id = i;					//为用户id编号
if (i <= capacity) {
line[(i - 1) % N + 1].push(customers[i]); ///啊这里是循环把前面第一波先到达的人放入队列中
}
else
behindQueue.push(customers[i]);   ////之后来的人都放入后面的队列里面去
}
int countC = K;			//k是客户数量,Q是客户查询数量
while (nowTime < CloseTime&&countC) {		//如果没到营业时间而且还有顾客来
nowTime++;								//每分钟更新一次系统????像是一个计时器
for (int i = 1; i <= N; i++) {			//每个队伍都查看一遍
if (!line[i].empty()) {				//如果该队伍不是空的
int donetime = line[i].front().processtime + lastTime[i];  //??????结束时间等于过程所需时间+上一个人时间??
int id = line[i].front().id;							  //更新一下下一个要执行服务的id
if ((donetime>CloseTime||donetime==nowTime)&&lastTime[i]<nowTime)
//如果现在就是当前窗口正在办理的顾客的结束时间且lasttime<nowtime
{
customers[id].donetime = donetime;		//给队首顾客结束时间;
lastTime[i] = donetime;			//lasttime就是上一个人结束的时间???

/*!!!!!!!*/
{line[i].pop();			//第一个顾客出去开始办理业务
countC--;				//需要办理业务的顾客数量减少
if (behindQueue.size())				//把后面长队的人调入前面
{
line[i].push(behindQueue.front());
behindQueue.pop();

}}

}
else if (lastTime[i] >= nowTime) {			//
line[i].pop();
countC--;
if (behindQueue.size())
{
line[i].push(behindQueue.front());
behindQueue.pop();

}
}
}
}
}
int query;
for (int i = 0; i < Q; i++)
{
scanf("%d", &query);
if (customers[query].donetime != -1)
printf("%02d:%02d\n",
8 + customers[query].donetime / 60,
customers[query].donetime % 60);
else
printf("sorry\n");
}

return 0;
}

然后我觉得还有第二种方法,但不知道为什么编译出问题了。有时间的话在重新研究一i下,毕竟这个才是自己想出来的思路,,上面是依照网络纠正得。

#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<queue>
#include<stack>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXSIZE 30

const int maxn = 21;
const int maxk = 1005;
int N, M, K, Q;
struct customer {
int id, servertime;
int begintime, endtime;
customer() :begintime(0), endtime(-1),
servertime(0), id(0) {};
}cus[maxk];
queue<customer>line[maxn], wait;

int main() {
cin >> N >> M >> K >> Q;
//int line_in = N * M;
for (int i = 1; i <= K; ++i)
{
cin >> cus[i].servertime;
cus[i].id = i;
if (i<=N) cus[i].endtime = cus[i].servertime;

if  (i<=N*M)line[(i - 1) % N + 1].push(cus[i]);
else  wait.push(cus[i]);
}

int countC = K;
int nowtime = 0, closetime = (17 - 8) * 60;

while (nowtime < closetime && countC) {
nowtime++;
for (int j = 1; j <= N; ++j){
if (line[j].empty()) {
continue;
}
customer head = line[j].front();
if (head.endtime <= nowtime || head.endtime > closetime) {
line[j].pop();
countC--;
line[j].front().begintime = head.endtime;
line[j].front().endtime = head.endtime + line[j].front().servertime;
cus[head.id].begintime = head.begintime;
cus[head.id].endtime = head.endtime;
if (!wait.empty()) {
line[j].push(wait.front());
wait.pop();
}
}
}
};

int q;
for (int i = 1; i <= Q; i++){
cin >> q;
if (cus[q].endtime == -1)cout << "sorry" << endl;
else printf("%02d:%02d\n", cus[q].endtime / 60 + 8, cus[q].endtime % 60);
}
return 0;

}
  • 点赞
  • 收藏
  • 分享
  • 文章举报
见鹿JOY 发布了16 篇原创文章 · 获赞 0 · 访问量 212 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: