• 约瑟夫问题


    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct LNode{
        int data;
        struct LNode *next;
    }LNode,*LinkList;
    
    LinkList Build(int n)//建立有n个结点的循环单链表 
    {
        int i;
        LinkList head;
        head = (LinkList)malloc(sizeof(LNode));
        LinkList s,p = head;
        p->data = 1;
        for(i = 2; i <= n; i ++)
        {
            s = (LinkList)malloc(sizeof(LNode));
            s->data = i;
            p->next = s;
            p = s;
        }
        p->next = head;
        return head;
    }
    LinkList GetElem(LinkList p,int n)//找到从head开始的第n个结点 
    {
        int i;
        LinkList s = p;
        for( i = 1; i < n; i ++)
        {
            s = s->next  ;
        }
        return s;
    }
    int Delete(LinkList p)//删除p的后继结点 
    {
        int num;
        LinkList s;
        s = (LinkList)malloc(sizeof(LNode));
        s = p->next ;
        p->next = s->next ;
        num = s->data ;
        free(s);
        return num;
     } 
    void CountOff(int n,int m,int k,LinkList head)//模拟循环报数全过程 
    {
        int i,num;
        LinkList p  = head;
    
        for(i= n; i >= 1; i --)
        {
            p = GetElem(p,m-1);
            num = Delete(p);
            if( i <= k)
                printf("%d ",num);
            p = p->next ; 
        }
    }
    int main()
    {
        int n,m,k;
        LinkList head;
        while(scanf("%d%d%d",&n,&m,&k)!=EOF)
        {
            head = Build(n);
            CountOff(n,m,k,head);
         } 
        return 0;
     } 
  • 相关阅读:
    Redis配置文件详解
    Redis基本操作-20150608
    Redis操作命令
    JedisPoolConfig配置
    jedis提供的功能
    配置Redis主从复制
    python数组查找算法---bisect二分查找插入
    python赋值和拷贝----一切皆对象,参数皆引用
    xml 解析 python
    进阶中级程序员需要做的事
  • 原文地址:https://www.cnblogs.com/hellocheng/p/7350138.html
Copyright © 2020-2023  润新知