uva133 The Dole Queue
2010-10-23 11:42
429 查看
#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;
}
#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;
}
相关文章推荐
- 算法竞赛入门经典(紫书)第四章—— The Dole Queue UVA-133
- UVA 133 The Dole Queue
- UVa 133 - The Dole Queue
- UVA【133】 The Dole Queue
- UVa 133 - The Dole Queue
- The Dole Queue(子过程设计)(UVa 133)
- UVA 133 - The Dole Queue
- uva 133 The Dole Queue 双向约瑟夫环 模拟实现
- UVA 133 The Dole Queue
- 紫书章四例题3——UVA 133 TheDole Queue
- UVA - 133 The Dole Queue
- The Dole Queue UVA - 133(依次抓走做夫妻肺片)
- 救济金发放(The Dole Queue, UVa 133)
- uva133-The Dole Queue
- Uva 133 - The Dole Queue
- 例题 4-3 救济金发放(The Dole Queue) UVa 133
- The Dole Queue UVA133
- uva 133 The Dole Queue(约瑟夫变形)
- UVA - 133 The Dole Queue(模拟链表)
- The Dole Queue UVA - 133