无
C代码如下:
#include<stdio.h> #include<stdlib.h> #include<malloc.h> typedef struct _node { struct _node* prev; struct _node* next; int number; }node,*linklist; linklist create(int len, int input_array[]); void array_iterate(int len, int input_array[], int m, int output_array[]); void display_array(int len, int array[]); int main() { int input_array[] = {12,1,34,56,28,9,17}; int len = sizeof(input_array)/sizeof(input_array[0]); int *output_array = (int *)malloc(len*sizeof(int)); int m; printf("please input m:"); scanf("%d",&m); printf("the original sequences are:"); display_array(len, input_array); array_iterate(len, input_array, m, output_array); return 0; } linklist create(int len, int input_array[]) { linklist head = (linklist)malloc(sizeof(node)); node *tail; int i=0; head->prev = head; head->next = head; head->number = input_array[i]; tail = head; for(i=1;i<len;i++) { node *p = (node*)malloc(sizeof(node)); p->number = input_array[i]; p->next = tail->next; p->prev = tail; tail->next = p; tail = p; head->prev = tail; } return head; } void array_iterate(int len, int input_array[], int m, int output_array[]) { linklist head; head = create(len, input_array); int i,j; node *p,*q; p = head; for(i=0; i<len; i++) { if(m == 1) { q = p->next; p->prev->next = q; q->prev = p->prev; m = p->number; output_array[i] = m; free(p); p = q; } else { for(j=1; j<m-1; j++) p = p->next; q = p->next; p->next = q->next; q->next->prev = p; m = q->number; output_array[i] = m; free(q); p = p->next; } } printf("the sequences of leaving the list are:"); display_array(len, output_array); } void display_array(int len, int array[]) { int i; for(i=0; i<len; i++) printf("%d ",array[i]); printf("\n"); }
注:网上很多有关约瑟夫的代码都没有考虑到m=1的情况。
几组测试用例的结果如下:
原文:http://blog.csdn.net/hs794502825/article/details/38013863