zoukankan      html  css  js  c++  java
  • 2020.8.21收获

    学习内容

    整数排序

    编写函数重载,分别将两个整数升序排列后输出、三个整数升序排列后输出、四个整数升序排列后输出

    复制代码
    import java.util.Scanner;
    public class Study {
        public static void sort(int a[], int n) {
            int temp;
            for (int i = 0; i < n - 1; i++) {
                for (int j = 0; j < n - i - 1; j++) {
                    if (a[j] > a[j + 1]) {
                        temp = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = temp;
                    }
                }
            }
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i] + " ");
            }
            System.out.println();
        }
        //两个整数排序
        public static void sort(int a, int b) {
            int arr[] = { a, b };
            sort(arr, 2);
        }
        //三个整数排序
        public static void sort(int a, int b, int c) {
            int arr[] = { a, b, c };
            sort(arr, 3);
        }
        //四个整数排序
        public static void sort(int a, int b, int c, int d) {
            int arr[] = { a, b, c, d };
            sort(arr, 4);
        }
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int a1, a2;
            int b1, b2, b3;
            int c1, c2, c3, c4;
            System.out.print("请输入两个整数:");
            a1 = in.nextInt();
            a2 = in.nextInt();
            System.out.print("排序后:");
            sort(a1, a2);
            System.out.print("请输入三个整数:");
            b1 = in.nextInt();
            b2 = in.nextInt();
            b3 = in.nextInt();
            System.out.print("排序后:");
            sort(b1, b2, b3);
            System.out.print("请输入四个整数:");
            c1 = in.nextInt();
            c2 = in.nextInt();
            c3 = in.nextInt();
            c4 = in.nextInt();
            System.out.print("排序后:");
            sort(c1, c2, c3, c4);
        }
    }
    复制代码

  • 相关阅读:
    单片机、嵌入式ARM学习网站推荐(多年的积累)
    单片机心得
    printf函数解析
    C语言数组与指针详解
    C语言数组与指针详解
    单片机心得
    单片机、嵌入式ARM学习网站推荐(多年的积累)
    嵌入式开发资料集锦
    poj1941
    poj1723
  • 原文地址:https://www.cnblogs.com/ltw222/p/14151397.html
Copyright © 2011-2022 走看看