美文网首页JavaPython3
Believer & non-believer

Believer & non-believer

作者: JaedenKil | 来源:发表于2019-07-29 14:56 被阅读21次

    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]

    相关文章

      网友评论

        本文标题:Believer & non-believer

        本文链接:https://www.haomeiwen.com/subject/htybrctx.html