64名运动员(编号为1-64)排成一列报数,报的数字为5的倍数则出列,求第N轮报数后仍在队伍中的运动员编号

题目描述

64名运动员(编号为1-64)排成一列报数,报的数字为5的倍数则出列,求第N轮报数后仍在队伍中的运动员编号


思路

直接模拟就完事


代码

import java.util.ArrayList;

public class Main {
    public static ArrayList<Integer> func(int N){
        ArrayList<Integer> res = new ArrayList<Integer>();

        for(int i=1;i<=64;i++)          //给运动员编号
            res.add(i);

        for(int i=1;i<=N;i++)
        {
            if(res.size()<5)    //如果已经不足5个人了,就不会报到5的倍数
                return res;

            int n =res.size();
            ArrayList<Integer> ans = new ArrayList<Integer>();

            for(int j=0;j<n;j++)  //模拟报数
            {
                if((j+1)%5==0){   //报到5的不放入新列表
                    continue;
                }
                ans.add(res.get(j));  //不报到5的放入新列表
            }

            res = ans;
        }

        return res;
    }
    public static void main(String[] args) {
        System.out.println(func(1));
        System.out.println(func(2));
        System.out.println(func(3));
        System.out.println(func(4));
        System.out.println(func(5));
        System.out.println(func(6));
        System.out.println(func(7));
        System.out.println(func(8));
        System.out.println(func(9));
        System.out.println(func(10));
        System.out.println(func(11));
        System.out.println(func(12));
        System.out.println(func(13));
        System.out.println(func(14));
        System.out.println(func(15));
        System.out.println(func(16));
    }
}

输出结果

[1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14, 16, 17, 18, 19, 21, 22, 23, 24, 26, 27, 28, 29, 31, 32, 33, 34, 36, 37, 38, 39, 41, 42, 43, 44, 46, 47, 48, 49, 51, 52, 53, 54, 56, 57, 58, 59, 61, 62, 63, 64]
[1, 2, 3, 4, 7, 8, 9, 11, 13, 14, 16, 17, 19, 21, 22, 23, 26, 27, 28, 29, 32, 33, 34, 36, 38, 39, 41, 42, 44, 46, 47, 48, 51, 52, 53, 54, 57, 58, 59, 61, 63, 64]
[1, 2, 3, 4, 8, 9, 11, 13, 16, 17, 19, 21, 23, 26, 27, 28, 32, 33, 34, 36, 39, 41, 42, 44, 47, 48, 51, 52, 54, 57, 58, 59, 63, 64]
[1, 2, 3, 4, 9, 11, 13, 16, 19, 21, 23, 26, 28, 32, 33, 34, 39, 41, 42, 44, 48, 51, 52, 54, 58, 59, 63, 64]
[1, 2, 3, 4, 11, 13, 16, 19, 23, 26, 28, 32, 34, 39, 41, 42, 48, 51, 52, 54, 59, 63, 64]
[1, 2, 3, 4, 13, 16, 19, 23, 28, 32, 34, 39, 42, 48, 51, 52, 59, 63, 64]
[1, 2, 3, 4, 16, 19, 23, 28, 34, 39, 42, 48, 52, 59, 63, 64]
[1, 2, 3, 4, 19, 23, 28, 34, 42, 48, 52, 59, 64]
[1, 2, 3, 4, 23, 28, 34, 42, 52, 59, 64]
[1, 2, 3, 4, 28, 34, 42, 52, 64]
[1, 2, 3, 4, 34, 42, 52, 64]
[1, 2, 3, 4, 42, 52, 64]
[1, 2, 3, 4, 52, 64]
[1, 2, 3, 4, 64]
[1, 2, 3, 4]
[1, 2, 3, 4]

你可能感兴趣的:(#,秋招/找实习笔试/面试编程汇总,java,算法)