user-0 提问: 关于约瑟夫出圈问题

回复:
user: user-0
content: 当时找工作的时候的问题....

user: user-0
content: 我看不懂这个程序，，，每一行输出，求解释一下

user: user-0

user: user-1
content: //假设有n个人团团围做，从第1个人开始数数，数到第m个人时候，第m个人出列， //然后继续从1开始数数，数到第m个人退出 #include <stdio.h> #include <conio.h> int i,k,t; int n,m; static char f[1001];//0该座位未出圈,1该座位已出圈 void main() {     while (1) {         printf("Input n m(1000>=n>=m>=1):");         fflush(stdout);         rewind(stdin);         if (2==scanf("%d%d",&n,&m)) {             if (1000>=n && n>=m && m>=1) break;         }     }     t=0;//已出圈总人数     i=1;//座位编号     k=1;//当前要数的数     while (1) {         if (0==f[i]) {             if (m==k) {                 t++;                 f[i]=1;                 printf("%3d ",i);                 if (0==t%10) printf("\n");                 if (t>=n) break;             }             k++;if (k>m) k=1;         }         i++;if (i>n) i=1;     }     cprintf("Press any key ...");     getch(); }

user: user-2
content: 几年前写的代码了，LZ试试 #include <stdio.h> #include <stdlib.h>  #define LEN 50 #define NUM 5  typedef struct node {     int num;     struct node *next; }node;  int main() {     int i;     int length;     node *p;     node *ptr;     node *first;      first = (node *)malloc(sizeof(node));     if (first == NULL) {         printf("malloc fail!\n");         return -1;     }     first->num = 1;     p = first;      for(i = 1; i < LEN; i ++) {         p->next = (node *)malloc(sizeof(node));         p = p->next;         p->num = i+1;     }     p->next = first;     p = p->next;      length = LEN;     p = first;     while (length > 1) {         if (NUM == 1) {              for (i = 0; i < LEN - 1; i ++) {                  ptr = p;                 p = p->next;                 free(ptr);             }             printf("the last one left is %d\n", p->num);             free(p);             return 0;         } else {             for (i = 0; i < NUM - 2; i++) {                 p = p->next;             }             ptr = p->next;             p->next = ptr->next;             p = p->next;             free(ptr);             length --;         }     }     printf("the last one is %d\n", p->num);     free(p);              return 0; }

user: user-3
content: #include"suanfa.h"  int main() { 	linklist p; 	int temp,number,n,index; 	printf("请输入人数:\n"); 	scanf("%d",&n); 	printf("输入第一个上线数：\n"); 	scanf("%d",&number);  	init(p); 	insert(p,n); 	//number--; 	for(index=1;index<=n;index++) 	{	number--; 		deletelink(p,number,temp); 		printf("%d ",number); 		number=temp; 	} 	return 0; } #include<stdio.h> #include<malloc.h>  typedef struct lnode{ 	int id; 	int password; 	struct lnode *next; }lnode,*linklist;  #define ok 1 #define error -1 typedef int status; #define size 1024  status init(linklist &l) { 	l=(linklist )malloc(sizeof(lnode)); 	if(!l)	return error; 	l->id=0; 	l->next=NULL; 	l->password=0; 	return ok; }  status insert(linklist &l,int n) { 	int index=1,array[size]; 	linklist p,s; 	printf("请输入%d个密码：\n",n); 	for(index=0;index<n;index++) 		scanf("%d",&array[index]);  	for(index=1,p=l;index<=n;index++) 	{ 		s=(linklist )malloc(sizeof(lnode)); 		s->id=index; 		s->password=array[index-1]; 		p->next=s; 		s->next=l->next; 		p=p->next; 	} 	return ok; }  status print(linklist l) { 	linklist p,q; 	for(p=l->next;p->next!=l->next;p=p->next) 	{ 		printf("号码是%d密码是%d ",p->id,p->password); 	} 	printf("\n"); 	return ok; }  status deletelink(linklist &l,int &n,int &password) { 	int index=1; 	linklist p=l,q; 	for(index=1;index<=n;index++) 		p=p->next; 	q=p->next; 	p->next=q->next; 	password=q->password; 	n=q->id; 	free(q); 	l=p; 	return ok; } 楼主自己修改吧，这代码是我参加学校的算法比赛的代码，当时没有完全调试成功，老师给个半勾，下来之后貌似成功了。

user: user-4
content: 这个和耶稣的叛徒问题类似。。。