美文网首页
约瑟夫环

约瑟夫环

作者: star_night | 来源:发表于2017-09-24 17:52 被阅读0次
#include<stdio.h>
typedef struct Node{
  int data;
  int id;
  struct Node *next;
}node;

#define SIZE sizeof(node)

node *initList();
void creatList(node *head, int n);
void printList(node *head);
node *jose(node *head,int password);
void printList_id(node *head);

int main(){
  int n,m;
  scanf("%d%d", &n, &m);
  node *head = initList();
  creatList(head,n);
  node *newhead = jose(head,m);
  printList_id(newhead);
}

//链表初始化
node *initList(){
  node *p;
  p = (node*)malloc(SIZE);
  p->next = p;
  return p;
}
//创建链表
void creatList(node *head, int n){
  node *new;
  node *pre;
  pre = head;
  int i = 1;
  while (n--) {
    new = (node*)malloc(SIZE);
    scanf("%d",&new->data);
    new->id = i;
    i++;
    pre->next = new;
    new->next = head;
    pre = new;
  }
}
//打印链表
void printList(node *head){
  node *cur = head->next;
  int i=0;
  while (cur != head) {
    i++;
    printf("i=%d,data=%d\n", i, cur->data);
    cur = cur->next;
  }
}

//打印链表id
void printList_id(node *head){
  node *cur = head->next;
  while (cur != head) {
    printf("%d ",cur->id);
    cur = cur->next;
  }
}
//判断空
int isEmptyList(node *head){
  if(head->next == head)
    return 1;
  return 0;
}
//约瑟夫环
node *jose(node *head,int password){
  node *newList = initList();
  node *pre = head;
  node *cur = head->next;
  node *Npre = newList;
  int count = 1;
  while (!isEmptyList(head)) {
    if(count == password){
      //重新计数
      password = cur->data;
      count = 0;
      //将cur从head链表中移除
      pre->next = cur->next;
      //将cur添加至newList
      Npre->next = cur;
      Npre = cur;
    }else{
      pre = cur;
    }
    cur = cur->next;
    //遍历至头节点
    if(cur == head){
      continue;
    }
    //head链表继续遍历
    count++;
  }
  Npre->next = head->next;
  head->next = head;
  Npre->next = newList;
  return newList;
}

#include<stdio.h>
typedef struct Node{
  int id;
  struct Node *next;
}node;

#define SIZE sizeof(node)

node *initList();
void creatList(node *head, int n);
node *jose(int n,int m);
void printList_id(node *head);

int main(){
  int n,m;
  scanf("%d%d", &n, &m);
  node *newhead = jose(n,m);
  printList_id(newhead);
}

//链表初始化
node *initList(){
  node *p;
  p = (node*)malloc(SIZE);
  p->next = p;
  return p;
}
//创建链表
void creatList(node *head, int n){
  node *new;
  node *pre;
  pre = head;
  int i = 1;
  while (n--) {
    new = (node*)malloc(SIZE);
    new->id = i;
    i++;
    pre->next = new;
    new->next = head;
    pre = new;
  }
}
//打印链表id
void printList_id(node *head){
  node *cur = head->next;
  while (cur != head) {
    printf("%d ",cur->id);
    cur = cur->next;
  }
}
//判断空
int isEmptyList(node *head){
  if(head->next == head)
    return 1;
  return 0;
}
//约瑟夫环
node *jose(int n,int m){
  node *newList = initList();
  node *head = initList();
  creatList(head,n);
  node *pre = head;
  node *cur = head->next;
  node *Npre = newList;
  int count = 1;
  while (!isEmptyList(head)) {
    if(count == m){
      //重新计数
      count = 0;
      //将cur从head链表中移除
      pre->next = cur->next;
      //将cur添加至newList
      Npre->next = cur;
      Npre = cur;
      //当cur指向head的下一个节点时
      if(cur == head->next)
        head->next = cur->next;
    }
    pre = cur;
    cur = cur->next;
    //遍历至头节点
    if(cur == head){
      continue;
    }
    //head链表继续遍历
    count++;
  }
  Npre->next = head->next;
  head->next = head;
  Npre->next = newList;
  return newList;
}

相关文章

  • 约瑟夫环问题

    约瑟夫环问题约瑟夫环描述:约瑟夫环(约瑟夫问题)是一个数学的应用问题:已知n个人(以编号1,2,3…n分别表示)围...

  • 循环单链表实现约瑟夫环(C语言)

    约瑟夫环

  • 约瑟夫环

    题目:100名学生围成一个圈, 编号从1到100,从第一名学生开始报数,从1-9报数 每报出9就退出,直到所有学生...

  • 约瑟夫环

  • 约瑟夫环

    问题:1~n个人围成一圈,从1开始报数,每次数到m这个人就出列,问最后剩下的是几号? 做法:递归。 假设剩下的是f...

  • 约瑟夫环

    解法一 用一个list模拟删除过程 解法二 数学公式,推到过程还没看懂

  • 约瑟夫环

    之前去面试的时候遇到这个问题,作为一只算法渣渣,自然带着恐惧的心情,然后自己瞎捣鼓了好长时间终于拼凑出来了一个很菜...

  • 约瑟夫环

  • 约瑟夫环

    复习一下关于约瑟夫环的实现原理: 如果用C来写的话,也会有许多的方法,比如1:采用链表(双向链表)2:递归3:队列...

  • 约瑟夫环

    公式法循环 公式法递归 链表法

网友评论

      本文标题:约瑟夫环

      本文链接:https://www.haomeiwen.com/subject/lltlextx.html