uva133--The Dole Queue

看到这个题,跟约瑟夫环感觉差不多,但是这个数据范围比较小,可以模拟实现,我用的是循环链表实现的过程。中间用到了标记,因为对于样例10 4 3来说,就会到后边只剩下7 10这两个节点形成的环,但是now_n这时候指向已删除的2节点,now_f指向已删除的6节点,然而,这就需要让now_n继续向后指,也指向6节点,这样就需要标记是否还在环内。

#include<stdio.h>
#include<iostream>
using namespace std;
struct node {

    int a;
    bool flg;       //mark the node is the part of the ring or not
    node *next;
    node *fron;
};
node *head, *endd;
void bui_cir_link ( int nn ) {      //form a ring with two-way linked list
    node *tmp, *temp;
    head = new ( node );
    head->a = 1;
    head->flg = 1;
    head->fron = NULL;
    head->next = NULL;
    tmp = head;
    for ( int i = 2; i <= nn; i++ )
    {
        temp = new node;
        tmp->next = temp;
        temp->fron = tmp;
        temp->a = i;
        temp->flg = 1;
        tmp = temp;
    }
    endd = tmp;
    endd->next = head;
    head->fron = endd;
}
void operate ( int nn, int kk, int mm ) {

    int n = nn, num = 0;
    node *now_f, *now_n;

    now_n = head;
    now_f = endd;
    while ( nn-- ) {

        if ( nn != n - 1 )      //specially operate the star  
            now_n = now_n->next;
        for ( int i = 1; i < kk; i++ ) {

            now_n = now_n->next;
        }
        if ( nn != n - 1 )
            now_f = now_f->fron;
        for ( int i = 1; i < mm; i++ ) {

            now_f = now_f->fron;
        }

        now_n->next->fron = now_n->fron;    //delete 
        now_n->fron->next = now_n->next;
        num++;                              //records the number of deleted node
        now_n->flg = 0;                     //mark


        printf("%3d",now_n->a);
        if ( num == n ) {
            break;
        }
        if ( now_n != now_f ) {

            now_f->fron->next = now_f->next;
            now_f->next->fron = now_f->fron;
            num++;
            now_f->flg = 0;


            printf("%3d",now_f->a);
            if ( num == n ) {
                break;
            }

        }
//        else
//           now_f= now_n;                    //delete the same node
        for ( int i = 0;; i++ ) {           //finding the closest deleted node with ring
            if ( !now_n->next->flg ) {

                now_n = now_n->next;
            }
            else
                break;
        }
        for ( int i = 0;; i++ ) {

            if ( !now_f->fron->flg ) {

                now_f = now_f->fron;
            }
            else
                break;
        }
        cout << ",";

    }

}
int main() {

    int n, m, k;
    while ( scanf ( "%d%d%d", &n, &k, &m ) != EOF && n && m && k ) {

        bui_cir_link ( n );
        operate ( n, k, m );
        cout << endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值