我的约瑟夫环,谁帮忙弄弄啊

来源:百度知道 编辑:UC知道 时间:2024/06/24 13:17:54
#include<stdio.h>
#include<malloc.h>
typedef struct node
{
int data;
struct node *next;
}listnode;
typedef listnode *linklist;
linklist head;

void main()
{
int t,m,n,i;
struct node *p,*q;
printf("please input the total stu num\n");
scanf("%d",&n);
printf("please input the out num\n");
scanf("%d",&m);
t=m%n;
head->data=1;
p=head;
for(i=1;i<=t;i++)
{
p->data=1;
p->next=(struct node *)malloc(sizeof(struct node));
p=p->next;
p->next=head->next;
}

while(p->next!=p)
{
if(p->next->data==t)
{
q=p->next;
p->next=q->next;
n--;
free(p);
}
}
printf("the last circle is :%d\n",p->data);
}
约瑟夫环,需要将所有的人的编号输出

#include<iostream>
using namespace std;
int jos(int n)
{
if (n==1)
return 1;
if (n%2 == 0)
return 2*jos(n/2)-1;
else
return 2*jos((n-1)/2)+1;
}
void main ()
{
int k;
scanf("%d",&k);
cout << jos(k);

}

直接一个递归就出来了嘛