TA的每日心情 | 开心 2014-6-18 08:29 |
---|
签到天数: 14 天 [LV.3]偶尔看看II
滴水大师
 
- 积分
- 2345
|
题目
解决代码及点评
- <pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"></pre><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code">/*
- 16.有17个人围成一圈(编号为0~16),从第 0号的人开始从 1报数,
- 凡报到 3的倍数的人离开圈子,然后再数下去,直到最后只剩下一个人为止。
- 问此人原来的位置是多少号?
-
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- void main()
- {
- int total = 17,need = 17,k = 3;
- int index = 0,count = 0,i = 0;
- int a[100] = {0};
- for (;;)
- {
- index = index % total + 1; // 取得第一个index
- if(a[index] == 0) // 如果上面有人
- {
- i = (i + 1) % k; // 找到报数3的人
- if(i == 0)
- {
- count++; // 退出
- a[index] = 1;
- }
- }
- if(count == need) break; // 退出的人够多时,整个循环退出
- }
- printf("index = %d\n",index);
- system("pause");
- }
- </pre><br><br><br></pre>
|
|