注意,当要删除的两个节点相邻时,counter= counter->right; clock= clock->left;
#include<stdio.h>
class Node
{
public:
int v;
Node *left, *right;
Node(int value,Node* l =0, Node* r=0)
{
v = value;
left = l;
right = r;
}
};
class Link
{
public:
Node* head;
int len;
Node* clock;
Node* counter;
Link(int n)
{
len = n;
head = new Node(1);
head->left = head;
head->right = head;
Node* pre = head;
for(int i = 2; i <= n; i++)
{
Node* p = new Node(i,pre,head);
pre->right = p;
head->left = p;
pre = p;
}
counter = head;
clock = head->left;
}
void dis()
{
Node* p = head;
for(int i = 0; i < len; i++)
{
printf("%d ",p->v);
p = p->right;
}
putchar('\n');
}
void connect(Node* l, Node* r)
{
l->right = r;
r->left = l;
}
void choose(int gap1, int gap2)
{
while(len)
{
for(int i = 0; i < gap1; i++, counter=counter->right)
continue;
for(int i = 0; i < gap2; i++, clock=clock->left)
continue;
Node* t1 = counter->left;
Node* t2 = clock->right;
if(t1 == t2)
{
len--;
if(len)
printf("%3d,", t1->v);
else
printf("%3d", t1->v);
connect(t1->left,t1->right);
delete t1;
}
else
{
len = len-2;
if(len)
printf("%3d%3d,", t1->v, t2->v);
else
printf("%3d%3d", t1->v, t2->v);
if(t1->right== t2)
{
counter= counter->right;
clock= clock->left;
}
connect(t1->left,t1->right);
connect(t2->left,t2->right);
delete t1;
delete t2;
}
}
}
};
int main()
{
//
freopen("input.txt","r",stdin);
int n,k,m;
while(scanf("%d%d%d", &n, &k, &m)==3)
{
if(n == 0) break;
Link link(n);
link.choose(k,m);
printf("\n");
}
}