uva133 The Dole Queue

简介: uva133 The Dole Queue
#include <stdio.h>#include <stdlib.h>#include <string.h>#define LOCAL#define MAXN 30typedefstructnode{
intdata;
structnode*pre, *next;
}*Node;
Nodelink[MAXN] ;
voidinit(intn, Node*head, Node*rear);
intmain()
{
intn, k, m;
Nodehead, rear;
intcount1, count2;
Nodep, q, r, s;
intfirst;
head= (Node)malloc(sizeof(structnode));
rear= (Node)malloc(sizeof(structnode));
#ifdef LOCALfreopen("c://uva_in.txt", "r", stdin);
#endifwhile (scanf("%d%d%d", &n, &k, &m) ==3&&!(n==0&&k==0&&m==0))
    {
init(n, &head, &rear);
p=head;
q=rear;
first=1;
count1=count2=1;
while(n>0)
        {
while (count1<k)
            {
p=p->next;
count1++;
            }
while (count2<m)
            {
q=q->pre;
count2++;
            }
if (p!=q)
            {
if (first)
first=0;
elseprintf(",");
printf("%3d%3d", p->data, q->data);
if (n>2)
                {
if (p->next!=q)
                    {
p->pre->next=p->next;
p->next->pre=p->pre;
r=p;
p=p->next;
free(r);
q->pre->next=q->next;
q->next->pre=q->pre;
r=q;
q=q->pre;
free(r);
                    } else                    {
p->pre->next=q->next;
q->next->pre=p->pre;
r=p;
s=q;
p=q->next;
q=r->pre;
free(r);
free(s);
                    }
                } elseif (n==2)
                {
free(p);
free(q);
                }
n-=2;
            }else            {
if (first)
first=0;
elseprintf(",");
printf("%3d", p->data);
if (n>1)
                {
p->pre->next=p->next;
p->next->pre=p->pre;
r=p;
p=p->next;
q=q->pre;
free(r);
                } elseif (n==1)
                {
free(p);
                }
n-=1;
            }
count1=count2=1;
        }
printf("/n");
    }
return0;
}
voidinit(intn, Node*head, Node*rear)
{
inti;
Nodep, q;
for (i=0; i<n; i++)
    {
p= (Node)malloc(sizeof(structnode));
p->data=i+1;
if (i==0)
        {
*head=p;
p->next=NULL;
q=p;
        } else        {
p->pre=q;
p->next=NULL;
q->next=p;
q=p;
        }
if (i==n-1)
*rear=q;
    }
    (*head)->pre=*rear;
    (*rear)->next=*head;
}
目录
相关文章
|
9月前
uva540 team queue
uva540 team queue
25 0
|
9月前
uva127 "Accordian" Patience
uva127 "Accordian" Patience
25 0
|
12月前
|
C++
【学习笔记】C++ stack和queue题目练习
设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。 实现 MinStack 类: MinStack(),初始化堆栈对象。 void push(int val) 将元素val推入堆栈。 void pop() 删除堆栈顶部的元素。 int top() 获取堆栈顶部的元素。 int getMin() 获取堆栈中的最小元素。
113 0
|
存储 索引
UVA12100 打印队列 Printer Queue
UVA12100 打印队列 Printer Queue
|
机器学习/深度学习 测试技术
UVA11175 有向图D和E From D to E and Back
UVA11175 有向图D和E From D to E and Back
UVA11175 有向图D和E From D to E and Back
|
人工智能 BI
【UVA 11997 K Smallest Sums】优先级队列
来自《训练指南》优先级队列的例题。 题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18702 题意:给定k个整数数组,各包含k个元素。
726 0