[Algoritmo 05] Ejercicios

1,

public class jiecheng {
    public static void main(String[] args) {
        jisuan(5);
    }
    public static void jisuan(int m){
        int sum = 0;
        int s = 1;
        for (int i = 1; i <= m;i ++){
            s = s * i;
            sum += s;
        }
        System.out.println(sum);
    }
}

Analizar gramaticalmente: 

2. Clasificación de selección

Requisito: Organizar la matriz dada en orden ascendente

Reglas: fije la primera posición en la matriz, compare el tamaño de los datos con la primera posición desde la segunda posición hasta la longitud total de la matriz, encuentre la posición de datos más pequeña e intercámbiela con la primera posición fija.

public class xuanzepl {
    public static void pl(int []arry){
        if(arry == null || arry.length < 2){
            return;//终止返回值为null的函数的运行
        }
        for(int i = 0;i < arry.length;i++){
            int min = i;
            for(int j = i + 1;j < arry.length;j++){
                min = arry[min] < arry[j] ? min : j;
            }
            jiaohuan(arry,i,min);
        }
    }
    public static void jiaohuan(int []arry,int i,int j){
        int temp = arry[i];
        arry[i] = arry[j];
        arry[j] = temp;
    }
    public static void shuchu(int []arry){
        for(int i = 0;i < arry.length;i++){
            System.out.print(arry[i] + " ");
        }
        System.out.println();
    }
    public static void main(String[] args) {
        int []arr = {5,7,3,9,8,3,2};
        shuchu(arr);
        pl(arr);
        shuchu(arr);
    }
}

3. Clasificación de burbujas

Requisito: Organizar la matriz dada en orden ascendente

Reglas: compare los datos en la primera posición con los datos en la segunda posición, coloque los datos con un valor grande en la segunda posición; luego compare los datos en la segunda posición con los datos en la tercera posición y coloque los datos con un valor grande en la tercera posición ... el último dígito debe ser el dato con el valor más grande.

public class xuanzepl {
//    public static void pl(int []arry){
//        if(arry == null || arry.length < 2){
//            return;//终止返回值为null的函数的运行
//        }
//        for(int i = 0;i < arry.length;i++){
//            int min = i;
//            for(int j = i + 1;j < arry.length;j++){
//                min = arry[min] < arry[j] ? min : j;
//            }
//            jiaohuan(arry,i,min);
//        }
//    }
    public static void jiaohuan(int []arry,int i,int j){
        int temp = arry[i];
        arry[i] = arry[j];
        arry[j] = temp;
    }
    public static void maopao(int []arry){
        if(arry == null || arry.length < 2){
            return;
        }
        for(int end = arry.length - 1;end >= 0;end--){
            for(int j = 1;j <= end;j++){
                if(arry[j - 1] > arry[j]){
                    jiaohuan(arry,j - 1,j);
                }
            }
        }
    }
    public static void shuchu(int []arry){
        for(int i = 0;i < arry.length;i++){
            System.out.print(arry[i] + " ");
        }
        System.out.println();
    }
    public static void main(String[] args) {
        int []arr = {5,7,3,9,8,3,2};
        shuchu(arr);
        maopao(arr);
        shuchu(arr);
    }
}

4. Clasificación por inserción

método uno:

public class xuanzepl {
//    public static void pl(int []arry){
//        if(arry == null || arry.length < 2){
//            return;//终止返回值为null的函数的运行
//        }
//        for(int i = 0;i < arry.length;i++){
//            int min = i;
//            for(int j = i + 1;j < arry.length;j++){
//                min = arry[min] < arry[j] ? min : j;
//            }
//            jiaohuan(arry,i,min);
//        }
//    }
    public static void jiaohuan(int []arry,int i,int j){
        int temp = arry[i];
        arry[i] = arry[j];
        arry[j] = temp;
    }
//    public static void maopao(int []arry){
//        if(arry == null || arry.length < 2){
//            return;
//        }
//        for(int end = arry.length - 1;end >= 0;end--){
//            for(int j = 1;j <= end;j++){
//                if(arry[j - 1] > arry[j]){
//                    jiaohuan(arry,j - 1,j);
//                }
//            }
//        }
//    }
    public static void charupx(int []arry){
        if(arry == null || arry.length < 2){
            return;
        }
        for(int end = 1;end < arry.length;end++){
            int newword = end;
            while(newword - 1 >= 0 && arry[newword - 1] > arry[newword]){
                jiaohuan(arry,newword,newword - 1);
                newword--;
            }
        }
    }
    public static void shuchu(int []arry){
        for(int i = 0;i < arry.length;i++){
            System.out.print(arry[i] + " ");
        }
        System.out.println();
    }
    public static void main(String[] args) {
        int []arr = {5,7,3,9,8,3,2};
        shuchu(arr);
        charupx(arr);
        shuchu(arr);
    }
}

Método dos:

public class xuanzepl {
//    public static void pl(int []arry){
//        if(arry == null || arry.length < 2){
//            return;//终止返回值为null的函数的运行
//        }
//        for(int i = 0;i < arry.length;i++){
//            int min = i;
//            for(int j = i + 1;j < arry.length;j++){
//                min = arry[min] < arry[j] ? min : j;
//            }
//            jiaohuan(arry,i,min);
//        }
//    }
    public static void jiaohuan(int []arry,int i,int j){
        int temp = arry[i];
        arry[i] = arry[j];
        arry[j] = temp;
    }
//    public static void maopao(int []arry){
//        if(arry == null || arry.length < 2){
//            return;
//        }
//        for(int end = arry.length - 1;end >= 0;end--){
//            for(int j = 1;j <= end;j++){
//                if(arry[j - 1] > arry[j]){
//                    jiaohuan(arry,j - 1,j);
//                }
//            }
//        }
//    }
    public static void charupx(int []arry){
        if(arry == null || arry.length < 2){
            return;
        }
        for(int end = 1;end < arry.length;end++){
            for(int pre = end - 1;pre >= 0 && arry[pre] > arry[pre + 1];pre--){
                jiaohuan(arry,pre,pre + 1);
            }
            
        }
    }
    public static void shuchu(int []arry){
        for(int i = 0;i < arry.length;i++){
            System.out.print(arry[i] + " ");
        }
        System.out.println();
    }
    public static void main(String[] args) {
        int []arr = {5,7,3,9,8,3,2};
        shuchu(arr);
        charupx(arr);
        shuchu(arr);
    }
}

Supongo que te gusta

Origin blog.csdn.net/qq_59942266/article/details/127682919
Recomendado
Clasificación