有N個人圍成一圈,順序排號,從第一個人開始報數(從1抱到M,M、N都從鍵盤輸入,凡報到M的人退出圈子。輸出要求:1、按退出順序輸出編號;2、并輸出最后留下來的人是原來的第幾號。

熱心網友

看我編的://circulator。cpp#include #include //循環表表元的結構體定義typedef struct node{int value;struct node * next;}NODE;//建立表元為1到n的循環表NODE * createList(int n){NODE * head,//循環表的頭指針* tail,//循環表末尾表元的指針* p;head = tail = NULL;for (int i = 1; i value = i;p-next = NULL;if (head == NULL) head = tail = p;else tail = tail-next = p;}tail-next = head;return head; }//刪除逢3的表元NODE * delNode(NODE * head){int count = 2;//計數器NODE * q = head,//q是p的前驅* p = head-next;//p指針與計數器count相對應while (p != q){if(count == 3){//數到3刪除該數字q-next = p-next;p-next = NULL;free(p);p = q-next;count = 1;}else{count++;q = q-next;p = p-next;}}return p;}void main(void){int n;NODE * h;printf("請輸入n:");scanf("%d", &n);h = createList(n);printf("最后剩下%d\n", delNode(h)-value);}。

熱心網友

#include typedef struct JNode { int Number; int Cipher; struct JNode *next; }JNode; int createJosephCycle(JNode **la,int n){ JNode *p,*s;int i; if((*la=(JNode *)malloc(sizeof(JNode)))==NULL){ printf("allocation failed(1)!");return 0; } (*la)-next = NULL; p = (*la); for(i=0;iCipher); s-Number = i+1; p-next = s; p=s; } p-next = (*la)-next; } int killOrder(JNode *la, int m){ JNode *p,*s; int j; p = la; j=0; while (p-next!=NULL) { while(jnext; j++; } if(p-next==p){ printf("%d GAME OVER!",p-Number); return 0; } if(j==m-1){ s = p-next; p-next = p-next-next; printf("%d ",s-Number); j=0; m = s-Cipher; } } } main(){ JNode *la; clrscr(); createJosephCycle(&la,1); killOrder(la,10); getch(); }。