{
"question_id": 1058505, "question_title": "报数", "question_content": "有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。", "difficulty": "简单", "answer_id": 1255809, "answer_content": "\n```\n#include<stdio.h>\nint main()\n{ \n int i,n,a[100],count,* p,out;\n\n printf(\"共有几个人围成一圈?(不超过100人)\\n\");\n\n scanf(\"%d\",&n);\n\n for(i=0;i<n;i++) //从1到n给每个人编号\n a[i]=i+1;\n\n i=0; //i为每次循环时的计数变量\n\n count=0; //这是按123报数时的计数变量\n\n out=0; //记录退出人数\n\n p=a;\n\n while(out<n-1) //循环直到只剩下一个人没退出为止\n {\n if(*(p+i)!=0) //遍历遇到没退出的人(序号没被标为0),123的报数+1\n count++;\n \n if(count==3) //当报数到3的时候,把退出的人的序号标为0,123的计数重新开始,退出人数+1\n {\n *(p+i)=0; \n\n count=0; \n\n out++;\n }\n \n i++; //计数变量+1,继续遍历\n \n if(i==n) //遍历完一遍后,有一部分人退出,接着遍历第二遍;\n i=0;\n }\n\n while(*p==0) //找出唯一一个不为0的数,即最后剩下的人的序号\n p++;\n\n printf(\"最后剩下的人是第%d号\\n\",*p);\n\n return 0;\n}\n\n```\nhttps://blog.csdn.net/a251844314/article/details/53302762\n\n# 问题解决的话,请点下采纳", "tag_name": "c语言", "cpp": "#include<stdio.h>\nint main()\n{ \n int i,n,a[100],count,* p,out;\n printf(\"共有几个人围成一圈?(不超过100人)\\n\");\n scanf(\"%d\",&n);\n for(i=0;i<n;i++) \n a[i]=i+1;\n i=0; \n count=0; \n out=0; \n p=a;\n while(out<n-1) \n {\n if(*(p+i)!=0) \n count++;\n if(count==3) \n {\n *(p+i)=0; \n count=0; \n out++;\n }\n i++; \n if(i==n) \n i=0;\n }\n while(*p==0) \n p++;\n printf(\"最后剩下的人是第%d号\\n\",*p);\n return 0;\n}", "topic_link": "https://bbs.csdn.net/topics/600469949", "status": 0, "keywords": "算法初阶,排序和顺序统计量,中位数和顺序统计量", "license": "csdn.net", "notebook": { "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/67.ipynb?type=file" }, "author": "zhaozhenhuan1103"
}