一个C语言编程题目,请高手指点
很简单,我定义了一个结构来模仿实际的人:#include <stdio。h>#define NUM 10 /*人数*/typedef struct _P{ int id;/*人的序号,也就是做的位置*/ int isOut;/*是否出局,0标识没有,1标识出局*/}P;void main(){ P p[NUM]; int count=0,out=NUM,count1=0; /*初始化*/ for(count=0;count<NUM;count++) { p[count]。 id=count+1; ...全部
很简单,我定义了一个结构来模仿实际的人:#include <stdio。h>#define NUM 10 /*人数*/typedef struct _P{ int id;/*人的序号,也就是做的位置*/ int isOut;/*是否出局,0标识没有,1标识出局*/}P;void main(){ P p[NUM]; int count=0,out=NUM,count1=0; /*初始化*/ for(count=0;count<NUM;count++) { p[count]。
id=count+1; p[count]。isOut=0; } count=0; /*开始循环踢人*/ while(out!=1)/*当剩下一人停*/ { for(count1=0;count1<3;)/*没有退出的人报数报到3退出*/ { if(p[count]。
isOut==0)count1++;/*只有没有退出的人可以报数*/ count++; if(count==NUM)count=0;/*检查是不是到尾了,如果是到尾就回到头*/ } count--; if(count<0)count=NUM-1; p[count]。
isOut=1;/*设置退出的人的退出标识为真*/ printf("%d ",p[count]。id); out--;/*退出的人数减1*/ } /*显示*/ for(count=0;count<NUM;count++) { if(p[count]。
isOut==0) { printf("the lase person id:%d\n",p[count]。
id); break; } }}在NUM为10的情况下的结果:3 6 9 2 7 1 8 5 10 the lase person id:4Press any key to continue。收起