1. 程式人生 > 其它 >【歸併排序】Acwing788.逆序對的數量

【歸併排序】Acwing788.逆序對的數量

public class Demo02 {
    public static void main(String[] args) {
        int max = max(10,10);
        System.out.println(max);

    }
    //比大小
    public static int max(int num1,int num2){
        int result = 0;
        if(num1==num2){
            System.out.println("num1=num2");
            return 0;//終止方法
        }
        if(num1>num2){
            result = num1;
        }else{
            result = num2;
        }
        return  result;

    }
}

過載:在一個類中,有相同的函式名稱,但形參不同的函式

方法的過載規則:
方法名稱必須相同
引數列表必須不同
方法的返回型別可以相同也可以不同
僅僅返回型別不同不足以成為方法的過載

public class Demo02 {
    public static void main(String[] args) {
        double max = max(10,10,10);
        System.out.println(max);

    }
    //比大小
    public static int max(int num1,int num2,int num3){
        int result = 0;
        if(num1==num2){
            System.out.println("num1=num2");
            return 0;//終止方法
        }
        if(num1>num2){
            result = num1;
        }else{
            result = num2;
        }
        return  result;

    }
    public static int max(int num1,int num2){
        int result = 0;
        if(num1==num2){
            System.out.println("num1=num2");
            return 0;//終止方法
        }
        if(num1>num2){
            result = num1;
        }else{
            result = num2;
        }
        return  result;

    }


}


可變長引數(只能放在引數的最後一位)

public class Demo04 {
    public static void main(String[] args) {
        Demo04 demo04 = new Demo04();
        demo04.test(1,2,3,4,5);
    }
    public void test(int...i){
        System.out.println(i[4]);
    }
}


遞迴
n!

public class Demo05 {
    public static void main(String[] args) {
        System.out.println(f(5));
    }
    public static int f(int n){
        if (n==1){
            return 1;
        }else{
            return n*f(n-1);
        }
    }

}