#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LOCAL
#define MAXN 30
typedef struct node
{
int data;
struct node *pre, *next;
}*Node;
Node link[MAXN] ;
void init(int n, Node *head, Node *rear);
int main()
{
int n, k, m;
Node head, rear;
int count1, count2;
Node p, q, r, s;
int first;
head = (Node)malloc(sizeof(struct node));
rear = (Node)malloc(sizeof(struct node));
#ifdef LOCAL
freopen("c://uva_in.txt", "r", stdin);
#endif
while (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;
else
printf(",");
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);
}
} else if (n == 2)
{
free(p);
free(q);
}
n -= 2;
}else
{
if (first)
first = 0;
else
printf(",");
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);
} else if (n == 1)
{
free(p);
}
n -= 1;
}
count1 = count2 = 1;
}
printf("/n");
}
return 0;
}
void init(int n, Node *head, Node *rear)
{
int i;
Node p, q;
for (i = 0; i < n; i++)
{
p = (Node)malloc(sizeof(struct node));
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;
}
uva133 The Dole Queue
最新推荐文章于 2022-02-25 19:38:17 发布