华为机试题 报数1,2,3 报到3 的出列,最后剩下的是谁

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

输出:多行,每行对应求的结果
思路:就是使用数组简单的模拟报数过程,报到3的标记为true

import java.util.Scanner;

public class baoshu4282 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n;
        while (sc.hasNext()) {
            n = sc.nextInt();
            boolean[] b = new boolean[n];
            int count = n;
            int t = 0;
            int i = 0;
            while (true) {
                for (i = 0; i < n;i++) {
                    if(b[i]==false)
                    {
                        t++;
                    }
                    if (t == 3) {
                        b[i] = true;
                        t = 0;
                        count--;
                        if (count == 1)
                            break;
                    }
                }
                if (count == 1)
                    break;
            }
            for(int k=0;k<n;k++){
                if(b[k]==false){
                    System.out.println(k+1);
                    break;
                }
            }
        }
    }

}
原文地址:https://www.cnblogs.com/todayjust/p/5343247.html