• 数据结构大实习——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;
    }
     
    

      

  • 相关阅读:
    OC中类的初始化
    iOS UITabBar简单使用
    iOS UIScrollView初体验
    iOS第一个Demo
    Xcode编写第一个iOS Demo(OC)及遇到的坑记录
    OC协议
    诚实的力量。Paul Graham (Y Combinator 创始人)关于诚实的评论。
    php5.4window下连接mssql
    mysql 游标嵌套循环实例
    关于支付宝接口文档的链接
  • 原文地址:https://www.cnblogs.com/wyhbadly/p/10131391.html
Copyright © 2020-2023  润新知