ecnu 2992 Queue

#include<bits/stdc++.h>
using namespace std;

//rubbish and his data structure
struct node{
    int v;
    node *next;
    node(int x=0):v(x),next(NULL){}
};

struct circular_queue{
    int maxSize,size;
    int *addr;
    int head,rear;
    circular_queue(int x):maxSize(x){}
    void build(){
        addr=(int*)malloc(sizeof(int)*maxSize);
        head=rear=size=0;
    }
    bool push(int x){
        if(size==maxSize)return false;
        addr[rear]=x;
        size+=1;
        rear=(rear+1)%maxSize;
    }
    int pop(){
        int u=addr[head];
        head=(head+1)%maxSize;
        size-=1;
        return u;
    }
};

int main(){
    int n,t;
    scanf("%d",&t);
    for(int k=0;k<t;k++){
        scanf("%d",&n);
        circular_queue q(n);
        q.build();
        for(int i=1;i<=n;i++)q.push(i);
        while(q.size>1){
            q.pop();
            q.push(q.pop());
        }
        printf("case #%d:\n%d\n",k,q.pop());
    }
}

猜你喜欢

转载自www.cnblogs.com/TAMING/p/9051402.html