user-0 提问: 关于出圈问题

回复:
user: user-1
content: 引用 9 楼 wander0001 的回复:[quote=引用 8 楼 zxh707wk 的回复:] [quote=引用 6 楼 wander0001 的回复:] [quote=引用 3 楼 zxh707wk 的回复:] 代码： #include <stdio.h> #include <malloc.h>  int fun(int sum,int n) { 	int* num=(int*)malloc(sizeof(int)*sum); 	int i; 	int j=sum; 	int m=0; 	for(i=0;j>1;i++) 	{ 		if(i>=sum)i=0; 		if(num[i]==0)m++; 		if(m==n) 		{ 			num[i]=1; 			m=0; 			j--; 		} 	} 	for(i=0;i<sum;i++) 	{ 		if(num[i]==0) 		{ 			free(num); 			return i+1; 		} 	} 	free(num); 	return -1; }  int main() { 	printf("%d\n",fun(5,3)); 	return 0; } 你的窝运行得不出结果。。你测试了吗？我想问问为什么我的代码得不到结果。。。[/quote] 哪没结果了？  没加getchar();而已[/quote]我用的是VS2008确实是没结果的[/quote] 他没赋初值，楼主看不出来么？

user: user-2
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-3
content: 我用linux gcc编译的。。。

user: user-0
content: 引用 8 楼 zxh707wk 的回复:[quote=引用 6 楼 wander0001 的回复:] [quote=引用 3 楼 zxh707wk 的回复:] 代码： #include <stdio.h> #include <malloc.h>  int fun(int sum,int n) { 	int* num=(int*)malloc(sizeof(int)*sum); 	int i; 	int j=sum; 	int m=0; 	for(i=0;j>1;i++) 	{ 		if(i>=sum)i=0; 		if(num[i]==0)m++; 		if(m==n) 		{ 			num[i]=1; 			m=0; 			j--; 		} 	} 	for(i=0;i<sum;i++) 	{ 		if(num[i]==0) 		{ 			free(num); 			return i+1; 		} 	} 	free(num); 	return -1; }  int main() { 	printf("%d\n",fun(5,3)); 	return 0; } 你的窝运行得不出结果。。你测试了吗？我想问问为什么我的代码得不到结果。。。[/quote] 哪没结果了？  没加getchar();而已[/quote]我用的是VS2008确实是没结果的

user: user-0
content: 引用 5 楼 qiang81020 的回复: #include <stdio.h>  #define area 2000  typedef struct pnode {     int mynum;     struct pnode *next; }node;  void printfnode(node *p) {     node *p0 = p;     while(p0->next != p)     {         printf ("%d ", p0->mynum);         p0 = p0->next;     }     printf ("%d ", p0->mynum); }  int foo(int m, int n, int *num) {     int i = 0, j = 0;     char strnum[5 + 1] = {0};     node *p, *phead;     node *ppre, *pnow;      if ((n < 1) || (n > 10))         return -1;     if (NULL == (p = (node *)malloc(sizeof(node) * m)))         return -1;      phead = p;     /*init*/     for (i = 0; i < m - 1; i++)     {         (p + i)->mynum = i + 1;         (p + i)->next  = p + i + 1;         printf ("(%d - 0x%08x)", (p + i)->mynum, p + i);     }     (p + m - 1)->mynum = m;     (p + m - 1)->next = p;     printf ("\n\n");      /*begin*/     ppre = p + m - 1;     pnow = p;     i = j = 1;     while (1)     {         if (i % n == 0)         {             printf ("REMOVE %d, i = %d\n", pnow->mynum, i);             pnow = pnow->next;             ppre->next = pnow;             if (pnow == ppre)                 break;             i++;             continue;         }          strnum[0] = '\0';         sprintf (strnum, "%05d", i);         if (n == strnum[4] - 0x30)         {             printf ("REMOVE %d, i = %d\n", pnow->mynum, i);             pnow = pnow->next;             ppre->next = pnow;             if (pnow == ppre)                 break;             i++;             continue;         }          ppre = ppre->next;         pnow = pnow->next;          if (2000 == i)             i = 1;         else             i++;     }     printf ("pnow->mynum = %d\n", pnow->mynum);     *num = pnow->mynum;      free (p);     return 0; }  int main(int argc, char **argv) {     int xx = 0;      if (0 != foo(1000, 7, &xx))         printf ("error!\n");     printf ("xx = %d\n", xx);      return 0; }  M = 10, N = 3; REMOVE 3, i = 3 REMOVE 6, i = 6 REMOVE 9, i = 9 REMOVE 2, i = 12 REMOVE 4, i = 13 REMOVE 7, i = 15 REMOVE 1, i = 18 REMOVE 10, i = 21 REMOVE 8, i = 23 xx = 5  M = 10, N = 7; REMOVE 7, i = 7 REMOVE 4, i = 14 REMOVE 8, i = 17 REMOVE 2, i = 21 REMOVE 1, i = 27 REMOVE 3, i = 28 REMOVE 9, i = 35 REMOVE 5, i = 37 REMOVE 6, i = 42 xx = 10  M = 20, N = 7; REMOVE 7, i = 7 REMOVE 14, i = 14 REMOVE 17, i = 17 REMOVE 1, i = 21 REMOVE 8, i = 27 REMOVE 9, i = 28 REMOVE 18, i = 35 REMOVE 20, i = 37 REMOVE 6, i = 42 REMOVE 15, i = 47 REMOVE 19, i = 49 REMOVE 12, i = 56 REMOVE 13, i = 57 REMOVE 10, i = 63 REMOVE 3, i = 67 REMOVE 11, i = 70 REMOVE 4, i = 77 REMOVE 5, i = 84 REMOVE 16, i = 87 xx = 2  用了个环链，顺序思路。  学习更好的方法留着慢慢看

user: user-0
content: 引用 3 楼 zxh707wk 的回复:代码： #include <stdio.h> #include <malloc.h>  int fun(int sum,int n) { 	int* num=(int*)malloc(sizeof(int)*sum); 	int i; 	int j=sum; 	int m=0; 	for(i=0;j>1;i++) 	{ 		if(i>=sum)i=0; 		if(num[i]==0)m++; 		if(m==n) 		{ 			num[i]=1; 			m=0; 			j--; 		} 	} 	for(i=0;i<sum;i++) 	{ 		if(num[i]==0) 		{ 			free(num); 			return i+1; 		} 	} 	free(num); 	return -1; }  int main() { 	printf("%d\n",fun(5,3)); 	return 0; } 你的窝运行得不出结果。。你测试了吗？我想问问为什么我的代码得不到结果。。。

user: user-3
content: 引用 6 楼 wander0001 的回复:[quote=引用 3 楼 zxh707wk 的回复:] 代码： #include <stdio.h> #include <malloc.h>  int fun(int sum,int n) { 	int* num=(int*)malloc(sizeof(int)*sum); 	int i; 	int j=sum; 	int m=0; 	for(i=0;j>1;i++) 	{ 		if(i>=sum)i=0; 		if(num[i]==0)m++; 		if(m==n) 		{ 			num[i]=1; 			m=0; 			j--; 		} 	} 	for(i=0;i<sum;i++) 	{ 		if(num[i]==0) 		{ 			free(num); 			return i+1; 		} 	} 	free(num); 	return -1; }  int main() { 	printf("%d\n",fun(5,3)); 	return 0; } 你的窝运行得不出结果。。你测试了吗？我想问问为什么我的代码得不到结果。。。[/quote] 哪没结果了？  没加getchar();而已

user: user-4
content: #include <stdio.h>  #define area 2000  typedef struct pnode {     int mynum;     struct pnode *next; }node;  void printfnode(node *p) {     node *p0 = p;     while(p0->next != p)     {         printf ("%d ", p0->mynum);         p0 = p0->next;     }     printf ("%d ", p0->mynum); }  int foo(int m, int n, int *num) {     int i = 0, j = 0;     char strnum[5 + 1] = {0};     node *p, *phead;     node *ppre, *pnow;      if ((n < 1) || (n > 10))         return -1;     if (NULL == (p = (node *)malloc(sizeof(node) * m)))         return -1;      phead = p;     /*init*/     for (i = 0; i < m - 1; i++)     {         (p + i)->mynum = i + 1;         (p + i)->next  = p + i + 1;         printf ("(%d - 0x%08x)", (p + i)->mynum, p + i);     }     (p + m - 1)->mynum = m;     (p + m - 1)->next = p;     printf ("\n\n");      /*begin*/     ppre = p + m - 1;     pnow = p;     i = j = 1;     while (1)     {         if (i % n == 0)         {             printf ("REMOVE %d, i = %d\n", pnow->mynum, i);             pnow = pnow->next;             ppre->next = pnow;             if (pnow == ppre)                 break;             i++;             continue;         }          strnum[0] = '\0';         sprintf (strnum, "%05d", i);         if (n == strnum[4] - 0x30)         {             printf ("REMOVE %d, i = %d\n", pnow->mynum, i);             pnow = pnow->next;             ppre->next = pnow;             if (pnow == ppre)                 break;             i++;             continue;         }          ppre = ppre->next;         pnow = pnow->next;          if (2000 == i)             i = 1;         else             i++;     }     printf ("pnow->mynum = %d\n", pnow->mynum);     *num = pnow->mynum;      free (p);     return 0; }  int main(int argc, char **argv) {     int xx = 0;      if (0 != foo(1000, 7, &xx))         printf ("error!\n");     printf ("xx = %d\n", xx);      return 0; }  M = 10, N = 3; REMOVE 3, i = 3 REMOVE 6, i = 6 REMOVE 9, i = 9 REMOVE 2, i = 12 REMOVE 4, i = 13 REMOVE 7, i = 15 REMOVE 1, i = 18 REMOVE 10, i = 21 REMOVE 8, i = 23 xx = 5  M = 10, N = 7; REMOVE 7, i = 7 REMOVE 4, i = 14 REMOVE 8, i = 17 REMOVE 2, i = 21 REMOVE 1, i = 27 REMOVE 3, i = 28 REMOVE 9, i = 35 REMOVE 5, i = 37 REMOVE 6, i = 42 xx = 10  M = 20, N = 7; REMOVE 7, i = 7 REMOVE 14, i = 14 REMOVE 17, i = 17 REMOVE 1, i = 21 REMOVE 8, i = 27 REMOVE 9, i = 28 REMOVE 18, i = 35 REMOVE 20, i = 37 REMOVE 6, i = 42 REMOVE 15, i = 47 REMOVE 19, i = 49 REMOVE 12, i = 56 REMOVE 13, i = 57 REMOVE 10, i = 63 REMOVE 3, i = 67 REMOVE 11, i = 70 REMOVE 4, i = 77 REMOVE 5, i = 84 REMOVE 16, i = 87 xx = 2  用了个环链，顺序思路。  学习更好的方法

user: user-5
content: 有一种简单做法： # include <iostream> using namespace std;  int main() { 	int i, s = 0; 	int M, N;  	cout << "input the number M and N:" << endl; 	cin >> M >> N;  	for (i = 2; i <= M; i++) 	{ 		s = (s + N) % i; 	}  	cout << "the number is:" << s + 1 << endl; } 数学推导的过程就不在这里写了

user: user-3
content: 代码： #include <stdio.h> #include <malloc.h>  int fun(int sum,int n) { 	int* num=(int*)malloc(sizeof(int)*sum); 	int i; 	int j=sum; 	int m=0; 	for(i=0;j>1;i++) 	{ 		if(i>=sum)i=0; 		if(num[i]==0)m++; 		if(m==n) 		{ 			num[i]=1; 			m=0; 			j--; 		} 	} 	for(i=0;i<sum;i++) 	{ 		if(num[i]==0) 		{ 			free(num); 			return i+1; 		} 	} 	free(num); 	return -1; }  int main() { 	printf("%d\n",fun(5,3)); 	return 0; }

user: user-3
content: 简单的问题要这么复杂吗。。。

user: user-0
content: 题目如下 M 个人围成一圈报数，数到 N （ 1<N<10 ）的倍数或包含 N 这个数字时出圈，问剩下的最后 一个人在原来的位置是多少？   报数规则：    1 、从第一个人开始报数为 1 ，下一个人报数为上一个人报数加 1   2 、报数的最大值为 2000 ，如果报数超过 2000 ，则下一个人重新从 1 开始报数   要求实现函数    int OutFunc (unsigned int iTotalNum, unsigned int iKey)   【输入】 iTotalNum ：   开始报数前的总人数 , 0<iTotalNum<65535                  iKey ：             题目中要求的数目 N  【输出】无    【返回】剩下的人的原来的位置   示例    输入： iTotalNum =5,    iKey =3  返回： 4     输入： iTotalNum =15,    iKey =3  返回： 10