无意间听到有这么个问题,于是第一想法就是用环形链表解决
约瑟夫斯问题(有时也称为约瑟夫斯置换),是一个出现在计算机科学和数学中的问题。在计算机编程的算法中,类似问题又称为约瑟夫环。
有个囚犯站成一个圆圈,准备处决。首先从一个人开始,越过
个人(因为第一个人已经被越过),并杀掉第k个人。接着,再越过
个人,并杀掉第k个人。这个过程沿着圆圈一直进行,直到最终只剩下一个人留下,这个人就可以继续活着。
问题是,给定了和
,一开始要站在什么地方才能避免被处决?
给出我的解:
/******************************************************************* Code Writer: EOF Date : 2014.02.14 Purpose: In computer science and mathematics, the Josephus Problem (or Josephus permutation) is a theoretical problem related to a certain counting-out game. There are people standing in a circle waiting to be executed. The counting out begins at some point in the circle and proceeds around the circle in a fixed direction. In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom. Touche me: If there are some problems in my codes, please touche me. My e-mail is jasonleaster@gmail.com .It is glad to know you. ********************************************************************/ #include<stdio.h> #include<stdlib.h> #define MOVENUMBER 2 //We skip MOVENUMBER people every time struct people { int location; struct people* next; }*header; struct people* creat_circle_list(const int ListSize);//Creat a circle-list for modelling people in the game void free_circle_list_t(struct people* const header,const int ListSize);//free the memory we allocated int game(struct people* const p_firt_man,int ListSize);//modelling the game. int main() { int number_of_people = 0; printf("Please enter the number of people in the room\n"); while(!scanf("%d",&number_of_people)) { printf("scanf error!\nPlease enter again!\n"); } if(creat_circle_list(number_of_people) == NULL) { printf("The application of creating a circle list is wrong!\nProcess End!\n"); free_circle_list_t(header,number_of_people); return 0; } else { game(header,number_of_people); } return 0; } struct people* creat_circle_list(const int ListSize) { int temp = 0; struct people* p_now = NULL;//pointer to current node struct people* p_before = NULL;//pointer to before node header = (struct people*)malloc(sizeof(struct people)); header->location = 0;//initialize header‘s loaction p_before = header; for(temp = 0;temp < ListSize-1;temp++) { p_now = (struct people*)malloc(sizeof(struct people)); p_now->location = temp+1; if(p_now == NULL)//If memory allocation is failed, end the function { return NULL; } p_before->next = p_now; p_before = p_now; } p_now->next = header;//link it as a circle return header; } void free_circle_list_t(struct people* const header,const int ListSize) { int temp = 0; struct people* p_now = header; struct people* p_before = NULL; struct people* p_next = NULL; struct people* p_free_people = NULL; for(temp =0;temp< ListSize;temp++) { p_free_people = p_now->next ; p_now->next = p_now->next->next; free(p_free_people); } } int game(struct people* const p_first_man,int ListSize) { int counter = 0; int num = 0; struct people* p_temp = NULL; struct people* p_before = p_first_man; for (counter = 0;counter < ListSize-1; counter++) { for(num = 0,p_temp = p_before;num < MOVENUMBER;num++)//move three people { p_before = p_temp; p_temp = p_temp->next; } if(p_temp == header) { header = p_temp->next; } p_before->next = p_temp->next; free(p_temp);//the people move out of the circle } printf("The last people lefted is %d\n",header->loaction); return 0; }
原文:http://blog.csdn.net/cinmyheart/article/details/19207097