JAVA企业面试题精选 Java基础 31-40

1.31.写Java代码,打印如下杨辉三角:

             1
           1  1
        1   2   1
     1   3   3   1
   1  4   6   4  1
1  5  10 10  5  1

参考答案:

public class Q031 {
    public static void main(String[] args) {
        int n = 6;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i; j++) {
                System.out.println(" ");
            }
            // 打印空格后面的字符,从第1列开始往后打印
            for (int j = 1; j <= i; j++) {
                System.out.print(num(i, j) + " ");
            }
        System.out.println();
    }
}

    public static int num(int x, int y) { // 第x行,第y行
        if (y == 1 || y == x) {
            return 1;
        }
        int c = num(x - 1, y - 1) + num(x - 1, y);
        return c;
    }
}
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

1.32.使用Java完成快速排序算法

参考答案:

public class QuickSort {
    // 排序方法,接受一个int[]参数,将会调用快速排序方法进行排序
    public static void sort(int[] number) {
        quickSort(number, 0, number.length - 1);
    }

    // 快速排序方法
    private static void quickSort(int[] number, int left, int right) {
        if (left < right) {
            int s = number[left];
            int i = left;
            int j = right + 1;
            while (true) {
                // 向右找大于s的数的索引
                while (i + 1 < number.length && number[++i] < s);
                // 向左找小于s的数的索引
                while (j - 1 > -1 && number[--j] > s);
                // 如果i>=j,退出循环
                if (i >= j) {
                    break;
                }
                // 否则交换索引i和j的元素
                swap(number, i, j);
            }
            number[left] = number[j];
            number[j] = s;
            // 对左边进行递归
            quickSort(number, left, j - 1);
            quickSort(number, j + 1, right);
        }
    }

    // 交换数组number中的索引为i、j的元素
    private static void swap (int[] number, int i, int j){
        int t;
        t = number[i];
        number[i] = number[j];
        number[j] = t;
    }

    public static void main(String[] args) {
        int[] num = { 34, 1, 23, 345, 12, 546, 131, 54, 78, 6543, 321, 85, 1234, 7, 76, 234};
        sort(num);
        for (int i = 0; i < num.length; i++) {
            System.out.println(num[i]);
        }
    }
}
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

1.33.设有n个人围成一圈,从第1个人开始报数,数到第m个人出列,然后从出列的下一个人开始报数,数到第m个人又出列,…,如此反复到所有人全部出列位置。设n个人的编号分别为1,2,…,n,打印出出列顺序

参考答案:

public class Q033 {
    private static boolean same(int[] p, int l, int n) {
        for (int i = 0; i < l; i++) {
            if (p[i] == n) {
                return true;
            }
        }
        return false;
    }

    public static void play(int playerNum, int step) {
        int[] p = new int[playerNum];
        int counter = 1;
        while (true) {
            if (counter > playerNum * step) {
                break;
            }
            for (int i = 1; i < playerNum + 1; i++) {
                while (true) {
                    if (same(p, playerNum, i) == false) {
                        break;
                    } else {
                        i = i + 1;
                    }
                }
                if (i > playerNum) {
                    break;
                }
                if (counter % step == 0) {
                    System.out.print(i + " ");
                    p[counter / step - 1] = i;
                }
                counter += 1;
            }
        }
        System.out.println();
    }

    public static void main(String[] args) {
        play(10, 7);
    }
}
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

1.34.写一段小程序检查数字是否为质数

参考答案:

public class Q034 {
    public boolean prime(int n) {
        if (n <= 0) {
            System.exit(0);
        }
        for (int i = 2; i <= n; i++) {
            for (int j = 2; j <= Math.sqrt(i); j++) {
                if ((n % j == 0) && (j != n)) {
                    return false;
                }
            }
        }
        return true;
    }
}
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

1.35.1到100自然数,放入到a[99]这个数组中,写一个函数找出没有被放进数组的那个数字

参考答案:

public class Q035 {
    public static void main(String[] args) {
        // 模拟a[99]数组
        int[] b = new int[99];
        for (int i = 0; i < 99; i++) {
            b[i] = i + 1;
        }
        int[] a = new int[100];
        for (int t : b) {
            a[t - 1] = t;
        }
        for (int t = 0; t < a.length; t++) {
            if (a[t] == 0) {
                System.out.println(t + 1);
            }
        }
    }
}
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

1.36.找出101到200自然数中的质数,for循环越少越好

参考答案:

public class Q036 {
    public static void main(String[] args) {
        for (int i = 101; i <= 200; i++) {
            boolean b = true;
            for (int n = 2; n < i; n++) {
                if (i % n == 0) {
                    b = false;
                    break;
                }
            }
            if (b) {
                System.out.println(i);
            }
        }
    }
}
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

1.37.用数组实现一个栈(Stack),至少有入栈方法push和出栈方法pop

参考答案:

import java.util.Arrays;

class Stack {
    private Object[] data; // 栈的内容
    private int size = 0; // 栈内的元素个数

    public Stack() {
        data = new Object[0];
    }

    // 判断栈是否满
    public boolean isFull() {
        // 当数组长度与栈内元素个数相同或者数组长度为0并且元素个数为0时
        return data.length == size || (data.length == 0 && size == 0);
    }

    // 判断栈是否empty
    public boolean isEmpty() {
        return size == 0;
    }

    // 数组扩容10个
    public void increData() {
        data = Arrays.copyOf(data, data.length + 10);
    }

    // 入栈操作
    public void push(Object obj) {
        if (isFull()){
            increData();
        }
        size++;
        data[size - 1] = obj;
    }

    // 出栈操作
    public Object pop() {
        Object o = data[size - 1];
        data[size - 1] = null;
        size--;
        return o;
    }
}

public class Q037 {
    public static void main(String[] args) {
        Stack stack = new Stack();
        stack.push(1);
        stack.push("123");
        Object o = stack.pop();
        System.out.println(o);
        o = stack.pop();
        System.out.println(o);
    }
}
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

1.38.a、b、c为3个整型变量,在不引入第4个变量的前提下写一个算法实现a=b、b=c、c=a?

参考答案:

public class Q038 {
    public static void main(String[] args) {
        int a = 1;
        int b = 2;
        int c = 3;
        a = b - a;
        b = b - a;
        a = a + b;
        b = c - b;
        c = c - b;
        b = c + b;
        System.out.println("a:" + a + "b:" + b + "c:" + c);
    }
}
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

1.39.Java编程写出1000-2000可以被3整除的数

参考答案:

public class Q039 {
    public static void main(String[] args) {
        for (int i = 1000; i <= 2000; i++) {
            if (i % 3 == 0) {
                System.out.println(i);
            }
        }
    }
}
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

1.40.有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

参考答案:

public class Q040 {
    public static void main(String[] args) {
        System.out.println("第1个月的兔子对数:1");
        System.out.println("第2个月的兔子对数:1");
        int f1 = 1, f2 = 1, f, M =24;
        for (int i = 3; i <= M; i++) {
            f = f2;
            f2 = f1 + f2;
            f1 = f;
            System.out.println("第" + i + "个月的兔子对数:" + f2);
        }
    }
}
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

1.31.写Java代码,打印如下杨辉三角:

             1
           1  1
        1   2   1
     1   3   3   1
   1  4   6   4  1
1  5  10 10  5  1

参考答案:

public class Q031 {
    public static void main(String[] args) {
        int n = 6;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i; j++) {
                System.out.println(" ");
            }
            // 打印空格后面的字符,从第1列开始往后打印
            for (int j = 1; j <= i; j++) {
                System.out.print(num(i, j) + " ");
            }
        System.out.println();
    }
}

    public static int num(int x, int y) { // 第x行,第y行
        if (y == 1 || y == x) {
            return 1;
        }
        int c = num(x - 1, y - 1) + num(x - 1, y);
        return c;
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

1.32.使用Java完成快速排序算法

参考答案:

public class QuickSort {
    // 排序方法,接受一个int[]参数,将会调用快速排序方法进行排序
    public static void sort(int[] number) {
        quickSort(number, 0, number.length - 1);
    }

    // 快速排序方法
    private static void quickSort(int[] number, int left, int right) {
        if (left < right) {
            int s = number[left];
            int i = left;
            int j = right + 1;
            while (true) {
                // 向右找大于s的数的索引
                while (i + 1 < number.length && number[++i] < s);
                // 向左找小于s的数的索引
                while (j - 1 > -1 && number[--j] > s);
                // 如果i>=j,退出循环
                if (i >= j) {
                    break;
                }
                // 否则交换索引i和j的元素
                swap(number, i, j);
            }
            number[left] = number[j];
            number[j] = s;
            // 对左边进行递归
            quickSort(number, left, j - 1);
            quickSort(number, j + 1, right);
        }
    }

    // 交换数组number中的索引为i、j的元素
    private static void swap (int[] number, int i, int j){
        int t;
        t = number[i];
        number[i] = number[j];
        number[j] = t;
    }

    public static void main(String[] args) {
        int[] num = { 34, 1, 23, 345, 12, 546, 131, 54, 78, 6543, 321, 85, 1234, 7, 76, 234};
        sort(num);
        for (int i = 0; i < num.length; i++) {
            System.out.println(num[i]);
        }
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

1.33.设有n个人围成一圈,从第1个人开始报数,数到第m个人出列,然后从出列的下一个人开始报数,数到第m个人又出列,…,如此反复到所有人全部出列位置。设n个人的编号分别为1,2,…,n,打印出出列顺序

参考答案:

public class Q033 {
    private static boolean same(int[] p, int l, int n) {
        for (int i = 0; i < l; i++) {
            if (p[i] == n) {
                return true;
            }
        }
        return false;
    }

    public static void play(int playerNum, int step) {
        int[] p = new int[playerNum];
        int counter = 1;
        while (true) {
            if (counter > playerNum * step) {
                break;
            }
            for (int i = 1; i < playerNum + 1; i++) {
                while (true) {
                    if (same(p, playerNum, i) == false) {
                        break;
                    } else {
                        i = i + 1;
                    }
                }
                if (i > playerNum) {
                    break;
                }
                if (counter % step == 0) {
                    System.out.print(i + " ");
                    p[counter / step - 1] = i;
                }
                counter += 1;
            }
        }
        System.out.println();
    }

    public static void main(String[] args) {
        play(10, 7);
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

1.34.写一段小程序检查数字是否为质数

参考答案:

public class Q034 {
    public boolean prime(int n) {
        if (n <= 0) {
            System.exit(0);
        }
        for (int i = 2; i <= n; i++) {
            for (int j = 2; j <= Math.sqrt(i); j++) {
                if ((n % j == 0) && (j != n)) {
                    return false;
                }
            }
        }
        return true;
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

1.35.1到100自然数,放入到a[99]这个数组中,写一个函数找出没有被放进数组的那个数字

参考答案:

public class Q035 {
    public static void main(String[] args) {
        // 模拟a[99]数组
        int[] b = new int[99];
        for (int i = 0; i < 99; i++) {
            b[i] = i + 1;
        }
        int[] a = new int[100];
        for (int t : b) {
            a[t - 1] = t;
        }
        for (int t = 0; t < a.length; t++) {
            if (a[t] == 0) {
                System.out.println(t + 1);
            }
        }
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

1.36.找出101到200自然数中的质数,for循环越少越好

参考答案:

public class Q036 {
    public static void main(String[] args) {
        for (int i = 101; i <= 200; i++) {
            boolean b = true;
            for (int n = 2; n < i; n++) {
                if (i % n == 0) {
                    b = false;
                    break;
                }
            }
            if (b) {
                System.out.println(i);
            }
        }
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

1.37.用数组实现一个栈(Stack),至少有入栈方法push和出栈方法pop

参考答案:

import java.util.Arrays;

class Stack {
    private Object[] data; // 栈的内容
    private int size = 0; // 栈内的元素个数

    public Stack() {
        data = new Object[0];
    }

    // 判断栈是否满
    public boolean isFull() {
        // 当数组长度与栈内元素个数相同或者数组长度为0并且元素个数为0时
        return data.length == size || (data.length == 0 && size == 0);
    }

    // 判断栈是否empty
    public boolean isEmpty() {
        return size == 0;
    }

    // 数组扩容10个
    public void increData() {
        data = Arrays.copyOf(data, data.length + 10);
    }

    // 入栈操作
    public void push(Object obj) {
        if (isFull()){
            increData();
        }
        size++;
        data[size - 1] = obj;
    }

    // 出栈操作
    public Object pop() {
        Object o = data[size - 1];
        data[size - 1] = null;
        size--;
        return o;
    }
}

public class Q037 {
    public static void main(String[] args) {
        Stack stack = new Stack();
        stack.push(1);
        stack.push("123");
        Object o = stack.pop();
        System.out.println(o);
        o = stack.pop();
        System.out.println(o);
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

1.38.a、b、c为3个整型变量,在不引入第4个变量的前提下写一个算法实现a=b、b=c、c=a?

参考答案:

public class Q038 {
    public static void main(String[] args) {
        int a = 1;
        int b = 2;
        int c = 3;
        a = b - a;
        b = b - a;
        a = a + b;
        b = c - b;
        c = c - b;
        b = c + b;
        System.out.println("a:" + a + "b:" + b + "c:" + c);
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

1.39.Java编程写出1000-2000可以被3整除的数

参考答案:

public class Q039 {
    public static void main(String[] args) {
        for (int i = 1000; i <= 2000; i++) {
            if (i % 3 == 0) {
                System.out.println(i);
            }
        }
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

1.40.有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

参考答案:

public class Q040 {
    public static void main(String[] args) {
        System.out.println("第1个月的兔子对数:1");
        System.out.println("第2个月的兔子对数:1");
        int f1 = 1, f2 = 1, f, M =24;
        for (int i = 3; i <= M; i++) {
            f = f2;
            f2 = f1 + f2;
            f1 = f;
            System.out.println("第" + i + "个月的兔子对数:" + f2);
        }
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

猜你喜欢

转载自blog.csdn.net/a639735331/article/details/81409570