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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章