用java队列实现报数问题

一些人围成一圈,用“123456789……”进行编号,依次按照“1-2-1-2-1-2”的顺序报数,喊到1的人出队,直到最后一个人出队,求最终出队顺序

兄弟,可做,具体题目

List  l[] = Arrays.aslist(new Integer[]{1,2,3,4,5.....})
fumc(l)

public void fumc(List ls){
       List<Integer> al = new ArrayList<>()
       for(i : ls){
            if(i%2 == 1) al.append(i)
       }
       if(al.size() > 1) fumc(al)
       else System.out.print(al[0])
}

手机打字实属不易,可能会有疏漏。大方向是没问题的

这个是数据结构中的约瑟夫环问题,但一般都是报数的最后一个人出队,你的题目稍加变动是报数的第一个人出队。
具体实现代码如下:

class Boy {
    private int no;
    private Boy next;
    
    //构造函数
    public Boy(int no) {
        this.no = no;
    }
    
    public int getNo() {
        return no;
    }
    public void setNo(int no) {
        this.no = no;
    }
    public Boy getNext() {
        return next;
    }
    public void setNext(Boy next) {
        this.next = next;
    }
    
}

public class Test {
    
    private static Boy first = null;
    
    public static void createLinkList(int num) {
        //先对传入的参数做个简单的校验
        if(num <1) {
            System.out.println("传入的节点个数不正确");
            return;
        }
        Boy curBoy = null;//创建辅助变量
        for(int i = 1; i <= num; i++) {
            Boy boy = new Boy(i);
            //如果是第一个节点
            if(i == 1) {
                first = boy;//让first指向第一个节点
                boy.setNext(boy);//单个节点强行成环
                curBoy = first;//辅助变量初始化,也指向第一个节点
            }else {
                //不是第一个节点
                curBoy.setNext(boy);
                boy.setNext(first);
                curBoy = boy;//注意辅助变量的后移
            }
            
        }
    }
    
    
    public static void josephu(int num) {
        int[] result = new int[num];
        int index = 0;
        //创建辅助变量
        Boy helper = first;
        //因为first一开始指向第一个节点,所以先让该辅助变量指向最后一个节点
        while(true) {
            if(helper.getNext() == first) {
                break;
            }
            //后移
            helper = helper.getNext();
        }
        //开始执行报数出列的循环过程
        while(true) {
            if(helper == first) {
                //如果只剩下一个节点了,就停止
                break;
            }
            //开始执行报数,让两个节点变量往后移count个位置,但实际只需要移动count-1次即可
            for(int j = 1; j <= 2; j++) {
                System.out.printf("第%d个小孩报数%d\n", first.getNo(), j);
                if(j == 1) {
                    System.out.printf("第%d个小孩出队\n", first.getNo());
                    result[index] = first.getNo();
                    index ++;
                }
                first = first.getNext();
                if(j == 1) {
                    helper.setNext(first);
                    helper = helper.getNext();
                }
            }
        }
        System.out.printf("最后一个小孩%d号出队\n", first.getNo());
        result[index] = first.getNo();
        //打印出队结果
        System.out.print("出队结果:");
        for (int item : result) {
            System.out.print(item + "、");
        }
    }
    
    public static void main(String args[]) {
        createLinkList(10);
        josephu(10);
    }

}

10个人报数的结果:

img

import java.util.*;

public class Test03 {
    public static void main(String[] args) {
        System.out.println("请数如参与活动的人数");
        final int number = new Scanner(System.in).nextInt();
        final Queue<Integer> integers = new LinkedList<>();
        for (int i = 0; i < number; i++) {
            integers.add(i + 1);
        }
        List<Integer> result = new ArrayList<>();
        int seq = 1;
        while (!integers.isEmpty()) {
            final Iterator<Integer> iterator = integers.iterator();
            while (iterator.hasNext()) {
                final Integer next = iterator.next();
                if (seq++ % 2 == 1) {
                    iterator.remove();
                    result.add(next);
                }
            }
        }
        System.out.println(result);
    }
}