约瑟夫问题(n个人围圈报数,报m出列,最后剩下的是几号?)
2018-02-27 09:44
218 查看
//n个人围圈报数,报m出列,最后剩下的是几号? #include <stdio.h> #include <stdlib.h> typedef struct node { int data; struct node *next; }node; node *create(int n) { node *p = NULL, *head; head = (node*)malloc(sizeof (node )); p = head; node *s; int i = 1; if( 0 != n ) { while( i <= n ) { s = (node *)malloc(sizeof (node)); s->data = i++; // 为循环链表初始化,第一个结点为1,第二个结点为2。 p->next = s; p = s; } s->next = head->next; } free(head); return s->next ; } int main() { int n = 41; int m = 3; int i; node *p = create(n); node *temp; m %= n; // m在这里是等于3 while (p != p->next ) { for (i = 1; i < m-1; i++) { p = p->next ; } printf("%d->", p->next->data ); temp = p->next ; //删除第m个节点 p->next = temp->next ; free(temp); p = p->next ; } printf("%d\n", p->data ); return 0; }
增加难度:
/*编号为1~N的N个人按顺时针方向围坐一圈,每人持有一个密码(正整数,可以自由输入), 开始人选一个正整数作为报数上限值M,从第一个人按顺时针方向自1开始顺序报数,报道M时停止报数。 报M的人出列,将他的密码作为新的M值,从他顺时针方向上的下一个人开始从1报数,如此下去,直至所有人全部出列为止。*/ #include <stdio.h> #include <stdlib.h> #define MAX_NODE_NUM 100 #define TRUE 1U #define FALSE 0U typedef struct NodeType { int id; int cipher; struct NodeType *next; } NodeType; /* 创建单向循环链表 */ static void CreaList(NodeType **, const int); /* 运行"约瑟夫环"问题 */ static void StatGame(NodeType **, int); /* 打印循环链表 */ static void PrntList(const NodeType *); /* 得到一个结点 */ static NodeType *GetNode(const int, const int); /* 测试链表是否为空, 空为TRUE,非空为FALSE */ static unsigned EmptyList(const NodeType *); int main(void) { int n, m; NodeType *pHead = NULL; while (1) { printf("请输入人数n(最多%d个): ", MAX_NODE_NUM); scanf("%d", &n); printf("和初始密码m: "); scanf("%d", &m); if (n > MAX_NODE_NUM) { printf("人数太多,请重新输入!\n"); continue; } else break; } CreaList(&pHead, n); printf("\n------------ 循环链表原始打印 -------------\n"); PrntList(pHead); printf("\n-------------删除出队情况打印 -------------\n"); StatGame(&pHead, m); } static void CreaList(NodeType **ppHead, const int n) { int i, iCipher; NodeType *pNew, *pCur; for (i = 1; i <= n; i++) { printf("输入第%d个人的密码: ", i); scanf("%d", &iCipher); pNew = GetNode(i, iCipher); if (*ppHead == NULL) { *ppHead = pCur = pNew; pCur->next = *ppHead; } else { pNew->next = pCur->next; pCur->next = pNew; pCur = pNew; } } printf("完成单向循环链表的创建!\n"); } static void StatGame(NodeType **ppHead, int iCipher) { int iCounter, iFlag = 1; NodeType *pPrv, *pCur, *pDel; pPrv = pCur = *ppHead; /* 将pPrv初始为指向尾结点,为删除作好准备 */ while (pPrv->next != *ppHead) pPrv = pPrv->next; while (iFlag) { for (iCounter = 1; iCounter < iCipher; iCounter++) { pPrv = pCur; pCur = pCur->next; } if (pPrv == pCur) iFlag = 0; pDel = pCur; /* 删除pCur指向的结点,即有人出列 */ pPrv->next = pCur->next; pCur = pCur->next; iCipher = pDel->cipher; printf("第%d个人出列, 密码: %d\n", pDel->id, pDel->cipher); free(pDel); } *ppHead = NULL; getchar(); } static void PrntList(const NodeType *pHead) { const NodeType *pCur = pHead; if (EmptyList(pHead)) return; do { printf("第%d个人, 密码: %d\n", pCur->id, pCur->cipher); pCur = pCur->next; } while (pCur != pHead); getchar(); } static NodeType *GetNode(const int iId, const int iCipher) { NodeType *pNew; pNew = (NodeType *)malloc(sizeof(NodeType)); if(!pNew) { printf("Error, the memory is not enough!\n"); exit(-1); } pNew->id = iId; pNew->cipher = iCipher; pNew->next = NULL; return pNew; } static unsigned EmptyList(const NodeType *pHead) { if(!pHead) { printf("The list is empty!\n"); return TRUE; } return FALSE; }
相关文章推荐
- C++[算法]用数组模拟约瑟夫问题,即 N个人围成一圈,顺时针每数到给定K值的人出列,直到剩下最后一个人,求出圈人的序号顺序
- 有n个人围成一圈,从第1个人开始,1、2、3报数,报至3出局,余下的人继续从1、2、3报数,问:最后剩下的一人是原来的第几号?同时求出被淘汰编号的序列。(要求:用循环队列解决该问题。)
- C语言 约瑟夫圈问题:N个人围成一圈,从第一个人开始按顺序报数并编号1,2,3,……N,然后开始从第一个人转圈报数,凡是报到3的退出圈子。则剩下的最后一个人编号是多少。
- 多个人围成一圈报数,数到固定值的人出列,求最后剩下的人
- n个人排成一圈,从1到3报数,数到3的人出列,输出最后剩下的哪个人是原来的第几号
- 环形链表 有m个人围成一圈,开始报数,报道n,退出,问最后剩下的是几号。
- 约瑟夫问题:有n个人围成一圈,顺序报数(1~3),报到3退出
- P123.32(有57个人围成一个圈,顺序排号,从第一个人开始报数(从1~4报数),凡报到四的人退出圈子,问最后留下的是原来的几号?)
- 有100个人围成一个圈,从1开始报数,报到14的这个人就要退出。然后其他人重新开始,从1报数,到14退出。问:最后剩下的是100人中的第几个人?
- 有10(100人或者n个)个人围成一圈,从第一个人开始报数(报1、2、3),只要报3的人就出去, 然后不停的循环报数,直到最后剩下一个人,计算出这个人最开始的位置。其实就是约瑟夫环问题
- n 个人围成一圈(编号1-n),数到3的出列,最后剩下的人的编号
- 文章标题n个人围成一圈,顺序排号。从第一个人开始报数(1~3),凡报到3的人退出圈子,问最后留下来的是原来几号的那位?
- N个人围成一圈,顺序排号,报号出列。求最后出来及剩下的孩纸?
- 有100个人围成一个圈,从1开始报数,报到14的这个人就要退出, 然后其他人重新开始,从1报数,到14退出。问:最后剩下的是100人中的第几个人?
- n个人围成一圈,顺序排号,从第1个人开始报数(1到3报数),凡报到3的人退出圈子,问最后留下的是原来几号
- 孩子们的游戏(圆圈中最后剩下的数)——约瑟夫问题
- 有 100 个人围成一个圈,从 1 开始报数,报到 14 的这个人就要退出。然后其他人重新开始, 从 1 报数,到 14 退出。问:最后剩下的是 100 人中的第几个人?
- 由n个人做成一排,按顺时针由1开始编号。然后由第一个人开始报数,数到m的人出局。求最后剩下的人的编号。
- 有n个人(1到n号)围成一个圈,从第1号按顺序报数(1-3),每报数为3的人从队伍去除,下个人继续从1开始报数,问最后剩下的那个人原来排在第几位- java
- 孩子们的游戏(圆圈中最后剩下的数)&约瑟夫问题I