joseph1.cpp

来自「图的创建和遍历」· C++ 代码 · 共 33 行

CPP
33
字号
// 约瑟夫问题,逐个输入密码。
#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 + =
减小字号Ctrl + -
显示快捷键?