设有n个人围坐一圈并按顺时针方向从1到n编号,从第s个人开始进行1到m的报数, 报数到第m个人, 此人出圈, 再从他的下一个人重新开始1到m的报数,如此进行下去直到所有的人都出圈为止。现要求按出圈次序,给出这n个人的顺序表p。 #include "stdafx.h" #include <stdlib.h> #include <stdio.h> typedef struct node { int data; struct node* next; }LNode; LNode* Create(int n, int k) //创建循环链表 { int start=k-1; if(start==0) start = n; LNode *s, *p, *L=NULL, *t; while(n!=0) { s = (LNode*)malloc(sizeof(LNode)); if(L==NULL) p=s; if(n==start) t=s; s->data = n; s->next = L; L=s; n--; } p->next = L; return t; } LNode* GetNode(LNode *p) { LNode *q; for(q=p;q->next!=p;q=q->next); q->next = p->next; free (p); return (q); } int Print(LNode *p, int m) { int i; printf("出队编号:\n"); while(p->next!=p) { for(i=1;i<=m;i++) { p=p->next; } printf("%3d",p->data); p = GetNode(p); } printf("%3d\n", p->data); return 0; } int _tmain(int argc, _TCHAR* argv[]) { LNode *p; int n,k,m; do { printf("输入总人数:"); scanf("%d",&n); }while(n<0); do { printf("输入开始人的序号1~%d:",n); scanf("%d",&k); }while(k<=0||k>n); do { printf("输入间隔数字:"); scanf("%d",&m); }while(m<=0); p=Create(n,k); Print(p,m); return 0; } |