📄 joseph1.cpp
字号:
// 约瑟夫问题,逐个输入密码。
#include <stdio.h>
#include <alloc.h>
typedef struct node{
int no,cipher;
struct node *next;
};
main(){
int m,n,i;
node *p,*q;
printf("\nInput total(<50) of peoples and start cipher(>0):");
scanf("%d %d",&n,&m);
p=(node *)malloc(sizeof(node)); p->next=p; //初始化
for(i=1;i<n;i++){
p->no=i;
printf("Enter %2d th person's cipher: ",i);
scanf("%d",&(p->cipher));
q=(node *)malloc(sizeof(node));
q->next=p->next; p->next=q; p=q;
}
p->no=n;
printf("Enter %2d th person's cipher: ",n);
scanf("%d",&(p->cipher));
printf("\nDequeue Order:\n");
for(i=1;i<=n;i++){
while(m>1){p=p->next; m--;}
q=p->next; p->next=q->next;
printf("%3d(%3d)",q->no,q->cipher);
m=q->cipher; free(q);
} return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -