zoukankan      html  css  js  c++  java
  • Java经典编程题50道之三十七

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

    public class Example37 {

        public static void main(String[] args) {
            f(1000);
        }
        public static void f(int n) {
            boolean[] arr = new boolean[n];
            for (int i = 0; i < arr.length; i++) {
                arr[i] = true;
            }
            int leftCount = n;
            int countNum = 0;
            int index = 0;
            while (leftCount > 1) {
                if (arr[index] == true) {
                    countNum++;
                    if (countNum == 3) {
                        countNum = 0;
                        arr[index] = false;
                        leftCount--;
                    }
                }
                index++;
                if (index == n) {
                    index = 0;
                }
            }
            for (int i = 0; i < n; i++) {
                if (arr[i] == true) {
                    System.out.println("原排在第" + (i + 1) + "位的人留下了。");
                }
            }
        }
    }

  • 相关阅读:
    Unity动态更换图片
    (特殊的)增删改查
    SQL Server 锁
    [转]排序规则
    [转]C#编写Windows服务程序图文教程
    [转]FreeTextBox使用详解 (版本3.1.1)
    [转]Newtonsoft.Json序列化和反序列
    C#性能优化实践(摘抄)
    一、PID控制原理
    POJ 2255已知二叉树前序中序求后序
  • 原文地址:https://www.cnblogs.com/qubo520/p/6962539.html
Copyright © 2011-2022 走看看