标签:style blog http color io os ar 数据 sp
1 /* 2 约瑟夫环问题(Josephus) 3 */ 4 #include <stdio.h> 5 #include <stdlib.h> 6 7 // 链表节点 8 typedef struct _RingNode 9 { 10 int pos; // 位置 11 struct _RingNode *next; 12 }RingNode, *RingNodePtr; 13 14 // 创建约瑟夫环,pHead:链表头指针,count:链表元素个数 15 void CreateRing(RingNodePtr pHead, int count) 16 { 17 RingNodePtr pCurr = NULL, pPrev = NULL; 18 int i = 1; 19 pPrev = pHead; 20 while(--count > 0) 21 { 22 pCurr = (RingNodePtr)malloc(sizeof(RingNode)); 23 i++; 24 pCurr->pos = i; 25 pPrev->next = pCurr; 26 pPrev = pCurr; 27 } 28 pCurr->next = pHead; // 构成环状链表 29 } 30 31 void PrintRing(RingNodePtr pHead) 32 { 33 RingNodePtr pCurr; 34 printf("%d", pHead->pos); 35 pCurr = pHead->next; 36 while(pCurr != NULL) 37 { 38 if(pCurr->pos == 1) 39 break; 40 printf("\n%d", pCurr->pos); 41 pCurr = pCurr->next; 42 } 43 } 44 45 void KickFromRing(RingNodePtr pHead, int m) 46 { 47 RingNodePtr pCurr, pPrev; 48 int i = 1; // 计数 49 pCurr = pPrev = pHead; 50 while(pCurr != NULL) 51 { 52 if (i == m) 53 { 54 // 踢出环 55 printf("\n%d", pCurr->pos); // 显示出圈循序 56 pPrev->next = pCurr->next; 57 free(pCurr); 58 pCurr = pPrev->next; 59 i = 1; 60 } 61 pPrev = pCurr; 62 pCurr = pCurr->next; 63 if (pPrev == pCurr) 64 { 65 // 最后一个 66 printf("\n%d", pCurr->pos); // 显示出圈循序 67 free(pCurr); 68 break; 69 } 70 i++; 71 } 72 } 73 74 int main() 75 { 76 int m = 0, n = 0; 77 RingNodePtr pHead = NULL; 78 printf("---------------Josephus Ring---------------\n"); 79 printf("N(person count) = "); 80 scanf("%d", &n); 81 printf("M(out number) = "); 82 scanf("%d", &m); 83 if(n <= 0 || m <= 0) 84 { 85 printf("Input Error\n"); 86 system("pause"); 87 return 0; 88 } 89 // 建立链表 90 pHead = (RingNodePtr)malloc(sizeof(RingNode)); 91 pHead->pos = 1; 92 pHead->next = NULL; 93 CreateRing(pHead, n); 94 #ifdef _DEBUG 95 PrintRing(pHead); 96 #endif 97 // 开始出圈 98 printf("\nKick Order: "); 99 KickFromRing(pHead, m); 100 printf("\n"); 101 }
标签:style blog http color io os ar 数据 sp
原文地址:http://www.cnblogs.com/dingyichao/p/4021418.html