zoukankan      html  css  js  c++  java
  • 一道java算法题

    12个人围成一圈,序号依次从1至12,从序号1开始顺时针依次数,数到7的人退出,下一个再依次从1开始数,求留下来的最后一个人的原始序号。

    public static void joseph(int[] array,int n) {
            for (int i = 0; i < array.length; i++) {
                array[i] = i+1;
            }
            // 计数器
            int counter = 0;
            // 剩余人数
            int leftCount = array.length;
            // 索引
            int index = 0;
            while (leftCount > 1) {
                if (array[index]>0) {
                    counter++;
                    if (counter == n) {
                        counter = 0;
                        array[index] = 0;
                        leftCount--;
                    }
                }
                index++;
                if (index == array.length) {
                    index = 0;
                }
            }   
            for (int i = 0; i < array.length; i++) {
                if (array[i]>0) {
                    System.out.println("剩余人员的位置是" + (i + 1));
                }
            }
        }
    
        public static void main(String[] args) {
            int [] array = new int[12];
            joseph(array,7);
        }
  • 相关阅读:
    jQuery-选择器及属性修改
    jQuery 基础理论
    CSS 之 BFC(块级格式化上下文)
    H5--Web Storage
    H5 -WebWorker
    H5 --拖放
    nodejs Multer中间件
    k8s pod
    kubernetes
    优化CUDA数据传输
  • 原文地址:https://www.cnblogs.com/root429/p/12799249.html
Copyright © 2011-2022 走看看