UVA 133

#include<iostream>
#include<cstdio>
#include<malloc.h>
#include<cstdlib>
using namespace std;
typedef struct  cicle
{
    int data;
    struct  cicle *next;
    struct cicle *prior;
}list;

list *createcyclelist(int n)
{
    list *head=NULL;
    list *s,*q;
    s=(list*)malloc(sizeof(list));
    head=s;
    q=s;
    head->data=0;
    int i;
    for(i=1;i<=n;i++)
    {
        s=(list*)malloc(sizeof(list));
        s->data=i;
        q->next=s;
        s->prior=q;
        q=s;
    }
    q->next=head;
    head->prior=q;
    return head;
}

int main()
{
    int N,k,m;
    while(cin>>N>>k>>m)
    {
        if(N==0&&k==0&&m==0)break;
        list *h1,*h2;
        h1=createcyclelist(N);
        h2=h1;
        int flag=N;
        while(flag)
        {
            int temp1=0,temp2=0;
            int i=1;
            while(true)
            {
                if(h1->data==0)
                    h1=h1->next;
                else
                {
                    h1=h1->next;
                    i++;
                }
                if(i==k&&h1->data!=0)
                {
                    temp1=h1->data;
                    break;
                }
            }
            int j=1;
            while(true)
            {
                if(h2->data==0)
                    h2=h2->prior;
                else
                {
                    h2=h2->prior;
                    j++;
                }
                if(j==m&&h2->data!=0)
                {
                    temp2=h2->data;
                    break;
                }
            }
            if(temp1!=temp2)
            {
                flag=flag-2;
                cout.width(3);
                cout<<temp1;
                cout.width(3);
                cout<<temp2;
                if(flag)cout<<",";
                h1->data=0;
                h2->data=0;
            }
            else
            {
                flag=flag-1;
                cout.width(3);
                cout<<temp1;
                if(flag)cout<<",";
                h1->data=0;
            }
        }
        cout<<endl;
    }
    return 0;
}

 

posted @ 2012-05-03 01:17  open your eyes  阅读(358)  评论(0编辑  收藏  举报