Java练习之:有10个人围成一圈,顺序排号。从第一个人开始报数,凡报到3的人退出

题目描述

有10个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。

示例

最后剩下的是4号

    序号  0 1 2 3 4 5 6 7 8 9
    人名  1 2 3 4 5 6 7 8 9 0
    报数  1 2 - 1 2 - 1 2 - 1
    报数  2 -   1 2   - 1   2
    报数  -     1 2     -   1
    报数        2 -         1
    报数        2           -
解题思路

1.设置每个人的初始状态为都在圈内,为true。如果报到3就被踢出圈,,为false
2.10个人得成环。用 index= (index + 1)% 人数得到每个人的下标,例如第10个人,下标为9,则它的index = 0。
类似于循环队列问题:https://blog.csdn.net/qq_43777322/article/details/89441884
3.每三个人是一个轮回,报的3 的人退出。用count表示每个人报的数,count =(count+1)%3
4.一直到只剩一个人时,循环结束。
5.找到那个状态还为true的人,输出他。

实现代码
测试用例
public static void main(String[] args) {
    System.out.println(callSurvivalNumber(10));
    System.out.println(callSurvivalNumber(1));
    System.out.println(callSurvivalNumber(0));
    System.out.println(callSurvivalNumber(-1));
}
具体方法
/**
 * @param count The total number of people in a circle
 * @return the last person
 */
public static int callSurvivalNumber(int count) {
    //1.校验合法性
    if (count <= 0) {
        throw new UnsupportedOperationException("can't into a circle");
    }
    //2.创建一个数目为count的boolean[],设置初始状态true,表明每个人都在圈里
    boolean[] peopleStates = new boolean[count];
    for (int i = 0; i < peopleStates.length; i++) {
        peopleStates[i] = true;
    }
    int index = -1;
    int counter = 0; //退出圈子的人数
    int current = 0; //当前人报的数
    while(counter < peopleStates.length - 1) {
        //3.先让其成环
        index = (index + 1) % peopleStates.length;
        //如果这个人已经退圈,就跳过他走向下一个人
        if (peopleStates[index] == false) {
            continue;
        }
        //4.index报的数为
        current = (current +1) % 3;
        if (current == 0) {
            peopleStates[index] = false;
            counter++;
        }
    }
    int i = 0;
    for (; i < peopleStates.length; i++) {
        if (peopleStates[i] == true) {
            return i+1;
        }
    }
    throw new RuntimeException("not found");
}
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值