Believer & non-believer

There are 30 persons, 15 are believers, 15 are non-believers, they plan to travel.
A bus can only carry 15 of them, the rest have to be left behind.
Someone suggests they stand in a circle and count, beginning from him, every time one counts to 9, he has to stay and quite counting.
Finally all the 15 believers will travel, all 15 non-believers have to stay.
The question: how do they stand in the line?
有30个人, 15个信徒, 15个非信徒, 他们计划去旅行。 但是大巴只能装下15个人, 剩下的人没法去, 得留下。
有人提议站成一个圆圈, 从他起, 轮流数数, 每个数到9的人就留下, 退出圆圈, 不再参与数数。
到最后剩下的15个人, 发现都是信徒, 那么问题是, 这30个人是如何站成一个圈的。

import java.util.ArrayList;

public class Believer {

    public static void main(String[] args) {
        ArrayList<Boolean> list = new ArrayList<>();
        for (int i = 1; i <= 30; i ++) {
            list.add(true);
        }

        /*
        true means believer
        false means nonBeliever
         */

        int remaining = 15; // remaining nonBeliever
        int index = 0; // the index of the list, should no be greater than 29
        int count = 0; // the number of the count, when count is 9, someone has to stay
        while (remaining > 0) { // when there are still non-believers
            if (list.get(index)) { // when counts to a non-believer, set to false, so the next round will ignore this element
                count ++;
                if (count == 9) {
                    list.set(index, false); // this is a non-believer
                    remaining --;
                    count = 0;
                }
            }
            index ++;
            if (index == 30) {
                index = 0;
            }
        }
        System.out.println(list);
    }

}

[true, true, true, true, false, false, false, false, false, true, true, false, true, true, true, false, true, false, false, true, true, false, false, false, true, false, false, true, true, false].


def main():
    circle = [True] * 30
    remaining = 15
    count = 0
    index = 0

    while remaining > 0:
        if circle[index]:
            count = count + 1
            if count == 9:
                circle[index] = False
                count = 0
                remaining = remaining - 1
        index = index + 1
        if index == 30:
            index = 0

    print(circle)


if __name__ == "__main__":
    main()

[True, True, True, True, False, False, False, False, False, True, True, False, True, True, True, False, True, False, False, True, True, False, False, False, True, False, False, True, True, False]

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,424评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,811评论 0 23
  • 本文转载自知乎 作者:季子乌 笔记版权归笔记作者所有 其中英文语句取自:英语流利说-懂你英语 ——————————...
    Danny_Edward阅读 43,943评论 4 38
  • 首先要感谢李如意老师的关心,我每天加班加点的学习就是因为有许许多多的问题没学明白 然后回家了也在看视频 查资料 你...
    90刘云鹏阅读 141评论 0 0
  • 我也坚信,一定有一群人。他们潜伏在各个阶层,各种职业。他们看似毫不费力,内心却异常勤奋,异常坚毅。他们不装样子给人...
    风过无痕ing阅读 156评论 0 1