• 猴子选大王


    public static void main(String[] args) {
    /*写有n个人围成一圈,顺序排号,从第一个人开始报数(从1~3报数),
    * 凡报到3的人退出圈子,问最后留下的人原来排在第几号。
    */
    int n = 8;//一共有8个人
    boolean[] arr = new boolean[n];
    for (int i = 0; i < n; i++) {
    arr[i] = true;//设置所有的数为true
    }
    int leftcount = n;//剩下的数
    int index = 0;// 递增,显示所有数据
    int countNum = 0;// 1--3循环
    while (leftcount > 1) {
    if (arr[index] == true) {
    countNum++;
    if (countNum == 3) {//判断是3的倍数
    countNum = 0;
    arr[index] = false;
    leftcount--;//当为false时,剩下的数-1;
    }
    }
    index++;
    if (index == n) {
    index = 0;//一次循环结束,开始下一次筛选
    }
    }
    for (int i = 0; i < n; i++) {
    if (arr[i] == true) {
    System.out.println("最后留下的人原来排在第" + (i + 1) + "号");
    }
    }
    }

  • 相关阅读:
    电脑技巧1
    web前端学习网站汇总1
    11月20日学习日志
    11月16日学习日志
    11月18日学习日志
    11月13日学习日志
    11月12日学习日志
    11月17日学习日志
    11月15日学习日志
    11月11日学习日志
  • 原文地址:https://www.cnblogs.com/sunda847882651/p/9478074.html
Copyright © 2020-2023  润新知