📄 joesph.cpp
字号:
/************************************************************
作者: 胡娟 200608204105.
工具: VC++ 6.0.
题目: Joseph环
/**********************************************************/
#include<stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct Node
{
int number;
int cipher;
struct Node *next;
}node,*hu;
hu H;//定义头结点为H;
void init(int n)
{
int i;
int cipher;
hu L;
if(n>=1)
{
scanf("%d",&cipher);
H=(hu)malloc(sizeof(node));//生成头结点;
H->number=1;
H->cipher=cipher;
H->next=H;
for(i=1;i<n;i++)
{
scanf("%d",&cipher);
L=(hu)malloc(sizeof(node));//生成副结点;
L->number=i+1;
L->cipher=cipher;
L->next=H->next;
H->next=L;
H=L;
}
H=H->next;//循环单链表的生成;
}
else
printf("你输入的人数错误!");
}
void Joseph(int m,hu h)//进行程序的循环,使顺序出列;
{
int i;
hu l;
l=h;
i=1;
while(i!=m)
{
i=i+1;
l=h;
h=h->next;
}
printf("%3d",h->number);
m=h->cipher;
l->next=h->next;
free(h);
h=l->next;
if(h!=l)
Joseph(m,h);
else
{
printf("%3d",h->number);
free(h);
}
}
void main()
{
int m;
int n;
system("cls");
printf("\n*********************Joseph环****************************\n");
printf("\n报数上限 : ");
scanf("%d",&m);
printf("总人数为: ");
scanf("%d",&n);
printf("%d个人的密码分别为:",n);
init(n);
printf("退出的人的编号顺序为:");
Joseph(m,H);
putchar('\n');
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -