输入三个整数x,y,z,请把这三个数由小到大输出的程序代码

admin 2022-09-27 PM 2150℃ 55条

在面试的笔试中,这个题目作为普通的编程题出现,考察的当然不仅仅是程序设计,还有考虑在实战中遇到类似问题时的应对思维。这里将几个写法主意分析。

写法一

public void sort(int x, int y, int z) {
        int[] arr = {x, y, z};
        for (int i = 1; i < arr.length; i++) {
            for (int j = 0; j < arr.length - i; j++) {
                if (arr[j] > arr[j+1]) {
                   int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
        for (int num : arr) {
            System.out.printf("%d ", num);
        }
    }

写法一使用冒泡排序,将无序的三个数排序后再输出。一般在面试中使用基础算法来解答题目,而且能够把基本算法写对,这个面试中是可以加分。

写法二

public void sort(int x, int y, int z) {
        if (x > y) {
            int temp = x;
            x = y;
            y = temp;
        } 
        if (x > z) {
            int temp = x;
            x = z;
            z = temp;
        }
        if (y > z) {
            int temp = y;
            y = z;
            z = temp;
        }
       System.out.printf("%d %d %d", x, y, z);   
}

写法二采用一般的程序设计,应该说是一个很入门的写法。如果在面试过程中想不到好办法,那么也可以写出来,但需要注意的是,千万不要写错,毕竟简单的入门题目还会写错,一旦写错将会被减分。

写法三

public static String outSortNum(int a, int b, int c) {
        int[] num = new int[]{a, b, c};
        Arrays.sort(num);
        return Arrays.toString(num);
    }

写法三很干脆,二话不说直接调用jdk的排序函数。这种写法在项目中最常见,当然可以写出来。不过一旦写出来,可能会被面试官问到sort函数中使用了什么排序算法等扩展问题。

综合以上写法,笔者推荐第一种写法,第二种次之,第三种的话尽量避免。

标签: 算法

非特殊说明,本博所有文章均为博主原创。

评论啦~



已有 55 条评论


  1. 1
    1

    1

    回复 2023-05-19 20:35
    1. 1
      1

      1

      回复 2023-05-19 23:58
      1. 1
        1

        555

        回复 2023-05-22 10:10
        1. 1
          1

          1

          回复 2023-05-22 13:59
        2. 1
          1

          1

          回复 2023-05-22 13:59
        3. 1
          1

          1

          回复 2023-05-22 13:59
        4. 1
          1

          1

          回复 2023-05-22 13:59
        5. 1
          1

          1

          回复 2023-05-22 14:00
        6. 1
          1

          1

          回复 2023-05-22 14:00
        7. 1
          1

          1

          回复 2023-05-22 14:00
        8. 1
          1

          1

          回复 2023-05-22 14:00
        9. 1
          1

          1

          回复 2023-05-22 14:00
      2. 1
        1

        1

        回复 2023-05-22 13:57
      3. 1
        1

        1

        回复 2023-05-22 13:58
      4. 1
        1

        1

        回复 2023-05-22 13:58
      5. 1
        1

        1

        回复 2023-05-22 13:58
      6. 1
        1

        1

        回复 2023-05-22 13:58
      7. 1
        1

        1

        回复 2023-05-22 14:01
      8. 1
        1

        1

        回复 2023-05-22 14:01
      9. 1
        1

        1

        回复 2023-05-22 14:01
      10. 1
        1

        1

        回复 2023-05-22 14:01
      11. 1
        1

        1

        回复 2023-05-22 14:01
    2. 1
      1

      1

      回复 2023-05-22 09:54
    3. 1
      1

      1

      回复 2023-05-22 13:50
    4. 1
      1

      1

      回复 2023-05-22 13:51
    5. 1
      1

      1

      回复 2023-05-22 13:51
    6. 1
      1

      1

      回复 2023-05-22 13:53
    7. 1
      1

      1

      回复 2023-05-22 13:53
    8. 1
      1

      1

      回复 2023-05-22 13:53
    9. 1
      1

      1

      回复 2023-05-22 13:53
    10. 1
      1

      1

      回复 2023-05-22 13:53
    11. 1
      1

      1

      回复 2023-05-22 13:54
    12. 1
      1

      1

      回复 2023-05-22 13:54
    13. 1
      1

      1

      回复 2023-05-22 13:54
    14. 1
      1

      1

      回复 2023-05-22 13:54
  2. 1
    1

    555

    回复 2023-05-19 22:00
    1. 1
      1

      1

      回复 2023-05-19 23:59
    2. 1
      1

      1

      回复 2023-05-19 23:59
    3. 1
      1

      1

      回复 2023-05-19 23:59
    4. 1
      1

      555

      回复 2023-05-20 00:44
  3. 1
    1

    1

    回复 2023-05-19 23:16
    1. 1
      1

      1

      回复 2023-05-20 00:17
  4. 1
    1

    1

    回复 2023-05-19 23:16
  5. 1
    1

    1

    回复 2023-05-19 23:16
  6. 1
    1

    1

    回复 2023-05-22 09:42
  7. 1
    1

    555

    回复 2023-05-22 10:21
    1. 1
      1

      1

      回复 2023-05-22 12:48
    2. 1
      1

      1

      回复 2023-05-22 12:48
    3. 1
      1

      1

      回复 2023-05-22 12:48
    4. 1
      1

      555

      回复 2023-05-22 14:45
    5. 1
      1

      555

      回复 2023-05-22 14:49
  8. 1
    1

    1

    回复 2023-05-22 12:47
  9. 1
    1

    1

    回复 2023-05-22 12:47
  10. 1
    1

    555

    回复 2023-05-22 14:40
  11. 1
    1

    555

    回复 2023-07-07 15:18