队列ALDS1_3-B:Queue


代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 100005;
int head,tail;
int n,q,c;
struct Node
{
	char name[15];
	int time;
}p[maxn];
void enqueue( Node x)
{
	p[tail] = x;
	tail = ( tail + 1 ) % maxn;
}
Node dequeue()  
{
	Node x = p[head];
	head = ( head + 1 ) % maxn;
	return x;
}
int main()
{
	int sum = 0;
	cin>> n >> q;
	for(int i=1;i<=n;i++)
	{
		scanf("%s",p[i].name);
		cin>>p[i].time;
	}
	head = 1;   tail = n+1;
	Node u;
	while( head != tail )  {
		u = dequeue();
		c = min( q ,u.time );    //执行时间片 q 或所需时间 u.time 的处理 
		u.time -= c;     //计算剩余的所需时间,在后面的操作中将其放在队列里 
		sum += c;        //累计已经经过的时间 
		if( u.time>0 )    enqueue(u);
		else  {
			cout<<u.name<<" "<<sum<<endl;
		}
	}
	return 0;
}
/*
5 100       // u.time=150
p1 150      //c=100
p2 80       //u.time -= c=50    u.time>0    enqueue(u)
p3 200      //u.time = 80
p4 350      //c=80
p5 20       //u.time -= c=0    不满足u.time>0  直接输出 
*/


用STL-queue实现此题:

点击打开链接

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值