您的位置:首页 > 理论基础 > 数据结构算法

数据结构——队列的使用(二)

2017-01-31 15:13 183 查看
AOJ ALDS1_3_B:Queue(可怜的我AOJ打不开,题目只能拍下来了..)

这个题目瞎bb的比较繁琐,大概意思就是——(举个例子)
   
规定一个人只能用100块耗时100秒,但是第一个人要用150,所以他用了100之后,拿着剩下的50到最后一个排队用去了。
第二个人只有80,所以他用完了就可以走了,但是他一共耗费了100秒+80秒=180秒。





这道题精妙在使用了环形缓冲区(使之不会超出内存)。



#include<stdio.h>
#include<string.h>
#define LEN 100005
typedef struct pp
{
char name[100];
int t;
}p;
p Q[LEN];
int head,tail,n;
void enqueue(p x)//向队列添加新元素
{
Q[tail]=x;
tail=(tail+1)%LEN;
}
p dequeue()//从队列开头取出元素
{
p x=Q[head];
head=(head+1)%LEN;
return x;
}
int min(int a,int b)
{
return a<b?a:b;
}
int main()
{
int elaps=0,c;
int i,q;
p u;
scanf("%d %d",&n,&q);
for(i=1;i<=n;i++)
{//按顺序将所有人物添加至队列
scanf("%s",Q[i].name);
scanf("%d",&Q[i].t);
}
head=1;tail=n+1;
while(head!=tail)
{
u=dequeue();
c=min(q,u.t);//执行时间片q或所需时间u.t的处理
u.t-=c;//所剩时间
elaps+=c;//累计已用时间
if(u.t>0)//如果处理尚未结束则重新添加至队列
enqueue(u);
else
printf("%s %d\n",u.name,elaps);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构 队列