⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 约瑟夫环.c

📁 已知n个人(以编号1
💻 C
字号:
#include"stdio.h"
#include"stdlib.h"
struct BackList
{
    int pos;
    int flag;
    struct BackList *next;
};

struct BackList *p,*q,*h;

void CreateList(int x)
{
    int i;
    h=p=malloc(sizeof(struct BackList));
    p->flag=0;
    p->pos=1;
    for(i=2;i<=x;i++)
    {
        q=malloc(sizeof(struct BackList));
        p->next=q;
        q->flag=0;
        q->pos=i;
        p=q;
    }
    p->next=h;
}

void Output(struct BackList *L,int m,int n,int s)
{
    int i;
    while(L->pos!=s)
    {
        L=L->next;
    }
    while(m>=1)
    {
        for(i=1;i<=n;i++)
        {
            if(L->flag==1)
            {
                L=L->next;
            }
            if(i%n==0)
            {
                if(L->flag==0)
                {
                    printf("%d\t",L->pos);
                    L->flag=1;
                    L=L->next;
                    m=m-1;
                }
            }
            if(i%n!=0)
            {
                L=L->next;
            }
        }
    }
}

void main()
{
    CreateList(4);
    Output(h,4,2,2);
    getch();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -