zoukankan      html  css  js  c++  java
  • 数据结构大实习——joseph

    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    
    using namespace std;
    
    const int maxn=1e5+10;
    
    typedef struct Queue
    {
    int front,rear;
    int num[maxn];
    } Queue;
    
    void InitQueue(Queue &q)
    {
    q.front=0;
    q.rear=0;
    return ;
    }
    
    bool QueueEmpty(Queue &q)
    {
    if((q.rear)%maxn==(q.front)%maxn)
    return true;
    else
    return false;
    }
    
    bool QueueFull(Queue &q)
    {
    if(q.front%maxn==(q.rear+1)%maxn)
    return true;
    else
    return false;
    }
    
    void QueuePush(Queue &q,int x)
    {
    q.num[q.rear]=x;
    q.rear=(q.rear+1)%maxn;
    return ;
    }
    
    void QueuePop(Queue &q)
    {
    q.front=(q.front+1)%maxn;
    return ;
    }
    
    int QueueTop(Queue &q)
    {
    return q.num[q.front];
    }
    
    typedef struct node
    {
    int no;
    node *next;
    }node;
    
    void init(node *&head,node *&last,int no)
    {
    head=(node *)malloc(sizeof(node));
    head->next=head;
    head->no=no;
    last=head;
    return ;
    }
    
    void insert(node *&last,int no)
    {
    node *tmp=(node *)malloc(sizeof(node));
    tmp->next=last->next;
    last->next=tmp;
    tmp->no=no;
    last=tmp;
    return ;
    }
    
    void solve(node *&head,node *&last,Queue &q,int m)
    {
    InitQueue(q);
    node *cur=head,*pre=last;
    int k=0;
    while(cur!=pre)
    {
    k++;
    if(k==m)
    {
    if(!QueueFull(q))
    QueuePush(q,cur->no);
    m=cur->no;k=0;
    node *tmp=cur;
    pre->next=cur->next;
    cur=cur->next;
    free(tmp);
    }
    else
    {
    pre=cur;
    cur=cur->next;
    }
    }
    QueuePush(q,cur->no);
    return ;
    }
    
    int main()
    {
    node *head,*last;
    int n,m;
    Queue q;
    while(cin>>n>>m)
    {
    for(int i=1; i<=n; i++)
    {
    i!=1?insert(last,i):init(head,last,i);
    }
    solve(head,last,q,m);
    for(int i=1;i<=n;i++)
    {
    i==n?cout<<QueueTop(q)<<endl:cout<<QueueTop(q)<<" ";
    if(!QueueEmpty(q))
    QueuePop(q);
    }
    }
    return 0;
    }
     
    

      

  • 相关阅读:
    支持向量机SVM知识点概括
    决策树知识点概括
    HDU 3081 Marriage Match II
    HDU 3572 Task Schedule
    HDU 4888 Redraw Beautiful Drawings
    Poj 2728 Desert King
    HDU 3926 Hand in Hand
    HDU 1598 find the most comfortable road
    HDU 4393 Throw nails
    POJ 1486 Sorting Slides
  • 原文地址:https://www.cnblogs.com/wyhbadly/p/10131391.html
Copyright © 2011-2022 走看看