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) + "位的人留下了。");
            }
        }
    }
}

时间: 2024-07-29 05:05:24

Java经典编程题50道之三十七的相关文章

Java经典编程题50道之四十七

读取7个数(1~50)的整数值,每读取一个值,程序打印出该值个数的*. public class Example47 {    public static void main(String[] args) {        int[] a = { 10, 7, 6, 15, 4, 3, 20 };        display(a);    } public static void display(int[] a) {        System.out.print("读取的整数有:"

Java经典编程题50道之十七

猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个:第二天早上又将剩下的桃子吃掉一半,而且又多吃了一个.以后每天早上都吃了前一天剩下的一半零一个.到第10天早上想再吃时,就只剩下一个桃子了.求第一天共摘了多少个桃子. public class Example17 {    public static void main(String[] args) {        int sum = peach(1);        System.out.println("第一天共摘

Java经典编程题50道之三十四

输入3个数a,b,c,按大小顺序输出. public class Example34 {    public static void main(String[] args) {        sort(5, 20, 15);    } public static void sort(int a, int b, int c) {        if (a < b) {            int t = a;            a = b;            b = t;        

Java经典编程题50道之三十五

有一个数组,将其最大的元素与第一个元素交换,最小的元素与最后一个元素交换,然后输出数组. public class Example35 {    public static void main(String[] args) {        int[] a = { 5, 7, 6, 1, 9, 4, 2, 3, 8 };        convertElement(a);    } public static void convertElement(int[] a) {        Syste

Java经典编程题50道之三十三

打印出杨辉三角形(要求打印出10行如下图)11 11 2 11 3 3 11 4 6 4 11 5 10 10 5 1 public class Example33 { public static void main(String[] args) {        yanghui(10);    } public static void yanghui(int n) {        int[][] a = new int[n][n];        for (int i = 0; i < n;

Java经典编程题50道之三十二

取一个整数a从右端开始的4-7位. public class Example32 {    public static void main(String[] args) {        cut(123456789);    } public static void cut(long n) {        String s = Long.toString(n);        char[] c = s.toCharArray();        int j = c.length;      

Java经典编程题50道之三十

有一个已经排好序的数组.现输入一个数,要求按原来的规律将它插入数组中. public class Example30 {    public static void main(String[] args) {        int[] m = { 3, 5, 9, 12, 16, 20, 25, 33 };        addElement(m, 17);    } public static void addElement(int[] a, int n) {        System.ou

Java经典编程题50道之三十九

写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度. public class Example39 {    public static void main(String[] args) {        length("Hello World!");    } public static void length(String s) {        int n = s.length();        System.out.println("输入的字符

Java经典编程题50道之三十六

有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数. public class Example36 {    public static void main(String[] args) {        int[] m = { 18, 12, 23, 34, 95, 76, 57, 28, 9 };        moveElement(m, 5);    } public static void moveElement(int[] m, int n) {