zoukankan      html  css  js  c++  java
  • 链表实现约瑟夫环

    //Dev c++

    #include<stdio.h>
    #include<malloc.h>
    #include<string.h>//调用putchar函数
    typedef struct stu{
    int num;
    struct stu*next;
    }stu,*pointer;
    int main()
    {
    int i,n,m,count;
    pointer p,q,r;
    r=p=q=(pointer)malloc(sizeof(pointer));
    p->num=1;p->next=NULL;//无头结点
    printf("Input n and m:");//m为步长
    scanf("%d%d",&n,&m);
    for(i=2;i<=n;i++)//建立链表
    {
    p=(pointer)malloc(sizeof(pointer));
    p->num=i;
    r->next=p;
    r=r->next;
    }
    p->next=q;//循环链表
    printf("出圈顺序为:\n");
    for(i=1,count=0;count<n-1;)//删除(n-1)个数
    {
    if(i==m)
    {
    printf("%d ",q->num);
    p->next=q->next;//删除q节点
    free(q);
    q=p->next;
    count++;
    i=1;
    }
    else
    {
    p=p->next;
    q=q->next;
    i++;//i++不能写在for循环内部
    }
    }
    printf("%d ",p->num);
    putchar('\n');
    system("pause");
    return 0;
    }

  • 相关阅读:
    51nod——T1267 4个数和为0
    cf220B莫队
    cf220b
    poj1436水平可见线
    poj2528贴海报,,
    poj3468
    hdu1698
    ural1989 单点更新+字符串hash
    cf Queries on a String
    hdu4605
  • 原文地址:https://www.cnblogs.com/hxsyl/p/2441385.html
Copyright © 2011-2022 走看看