zoukankan      html  css  js  c++  java
  • 数组习题练习0424

    数组练习题


    题目更新履历:

    1. 在指定位置插入指定元素 0424

    2.杨辉三角 0424

    3.在数组中返回所有目标值的下标0426,增加优化方案0427

    4.数组排序-选择排序0426

    5.选择排序-冒泡排序0426

    6.数组逆序0426


    1. 在指定位置插入指定元素【难点】

    存在一个数组,数组中的元素为 int[] array = {1, 3, 5, 7, 9, 11, 13, 15, 17, 0};

    要求 :

      1. 0是无效元素,仅占位使用

      2. 当前数组中【有效元素】个数为9 需求

      3.在该数组中的指定下标位置放入指定元素

    /*
    方法分析
        固定格式:
            public static 
        返回值:
            void 无需返回值
        方法名:
            insert
        形式参数列表
            三个:
                数组 int[] array
                位置 index 
                元素 value
    方法声明
        public staitc void insert(int[] array , int index , int value) {}
    
    */
    
    class HomeWorkc {
        public static void main(String[] args) {
            int[] array = {1, 3, 5, 7, 9, 11, 13, 15, 17, 0};
            
            int index = 7;
            int value = 6;
            insert(array , index , value);
            
        }
        
        /**
        * 该方法是在对应数组中指定位置插入指定数值
        *
        * @param int[] arr 传入的数组
        * @param int index 传入的数据的位置
        * @param int value 传入的数据数值
        */
        
        
        public static void insert(int[] arr , int index , int value) {
            if (index < 0 || index > arr.length) {
                System.out.println("输入位置错误");
                //return;  增加else模块,减少return
            } else {
    
                //新插入的元素位置:
                //1.在现有有效数字中间插入,此时就要求插入的位置index所对应的
                //数组中下标为index的元素被腾空(复制到后面的下标中)
                //2.在现有有效元素的后面位置插入            
    
                //i的初始值是9,也是数组的有效元素的个数
                //i-1对应的就是最后一个有效元素在数组中的下标
    
                //最后一步移位的操作是 index = (i-1)
                //for循环中,从后往前移
                for (int i = 9 ; i > index ; i--) {//i-1操作的是要被腾空的位置
                    arr[i] = arr[i-1];
                }
                arr[index] = value;
                
                
            }
            
            for (int i = 0 ; i < arr.length ;i++) {
                System.out.println(arr[i]);
            }
            
            return;
        }
        
    
    }
    public class HomeWork04261 {
        public static void main(String[] args) {
            int[] arr = {1, 3, 4, 5, 6, 7, 6, 3, 5, 7, 7, 7};//3-8
            
            
            // 创建与原数组长度一致的数组,用于保存查找值。然后将数组的有效
            // 长度(相同的合数)返回,循环打印结果
            int[] arrIndex = new int[arr.length];//
            
            int find = 5;
            // 接受方法的返回值,得到的数查询目标数据的个数
            int count = findArrIndex(arr , arrIndex , find);
            
            
            //增加返回值数据判断,根据返回结果给用户消息,避免没有结果现象
            if (count > 0) {
                for (int i = 0;i < count;i++) {
                    System.out.println(arrIndex[i]);
                }
            } else {
                System.out.println("Not Found");
            }
            
            
            
        }
        
        public static int findArrIndex(int[] arr , int[] arrIndex , int find) {
            
            //增加数组长度判断,杜绝可能出现异常。对用户输入数据进行合法性判断
            if (arr.length < arrIndex.length) {
                System.out.println("Input Parameter is Invalid");
                return 0;//不论是输入数组错误还是数组长度不对,返回0表示异常;
            }
            
            //设置一个累加器,用以累加个数           //也可将记录下标的数组元素全部设置“-1”,程序检索完返回后,截止到“-1”就停止打印。(因素原数组和目标值相同的元素下标位置不可能是负值)
            int count = 0;
            for ( int i = 0;i < arr.length; i++) {
                if (find == arr[i]) {
                    arrIndex[count] = i;
                    count++;
                }
            }
            return count;
            
        }
    
    }

     2.杨辉三角

    package Array;
    
    public class YanghuiTri {
        public static void main(String[] args) {
            //杨辉三角层数
            int n = 10;
            
            yanghuiTri(n);
        }
        /**
         * 杨辉三角
         * 
         * @param n int类型,杨辉三角的层数
         */
        public static void yanghuiTri(int n) {
            int[][] arr = new int[n][];
            
            for (int i = 1; i <= n; i++) {
                arr[i-1] = new int[i];
                arr[i-1][0] = 1;
                arr[i-1][i-1] = 1;
            }
            
            if (n > 2) {
                for (int i = 2; i < n; i++) {
                    for (int j = 1; j < i; j++) {
                        arr[i][j] = arr[i-1][j] + arr[i-1][j-1];
                    }
                }
                
            }
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < arr[i].length; j++) {
                    System.out.print(arr[i][j] + " ");
                }
                System.out.println();
            }
            
            
        }
        
    }

     运行结果:

    1 
    1 1 
    1 2 1 
    1 3 3 1 
    1 4 6 4 1 
    1 5 10 10 5 1 
    1 6 15 20 15 6 1 
    1 7 21 35 35 21 7 1 
    1 8 28 56 70 56 28 8 1 
    1 9 36 84 126 126 84 36 9 1 

    3.指定元素在指定数组中所有下标位置

    /* 
    3.找出指定元素在指定数组中所有下标位置 【难点】
        案例数组:
            int[] arr = {1, 2, 3, 1, 2, 3, 1, 2, 3};
            找出元素1 的所有下标位置
            需要得到:
                0, 3, 6
        要求:
            a. 不允许在方法内打印展示
            b. 考虑多个数据情况
            c. 需要在方法外获取到下标数据信息
            d. 不允许使用数组作为返回值
            e. 一个方法完成
    
     */
    public class HomeWork0426 {
        public static void main(String[] args) {
        
            int[] arr = {1, 2, 3, 1, 2, 3, 1, 2, 3};//目标数组
            int target = 1;//查找目标值
            
            int[] arrCount = new int[arr.length];//记位数组,用于记录查找目标值的下标
            int arrCountPos = 0;//控制记位数组插入位置
            
            int index = 0;//从目标数组的哪一个下标开始查找
            
            for (int i = index;i < arr.length;i++) {
                
                int temp =     findNumPos(arr, index, target);//把目标数组,当前查找值,查找目标值传给findNumPos方法
                
                
                //如果findNumPos返回值为-1,则说明数组中已经不存在和目标值相同的数字,跳出循环
                if (temp == -1) {
                    break;
                }
                
                index = temp +1;//把已经符合目标值的下标+1,然后赋值给index准备下一次查找
                
                //将符合目标的下标存在arrCount数组中,同时数组计数变量+1
                arrCount[arrCountPos] = temp;
                arrCountPos++;
            }
            
            //循环打印arrCount,把符合目标的数值打印出来
            for (int i = 0; i < arrCountPos; i++) {
                System.out.println(arrCount[i]);
            }
        }
        /**
         * 该方法用于找到目标数组中和目标值数值相同元素的下标,如果找到则返回下标值,未找到则返回-1
         * @param arr int类型数组,查找目标数组
         * @param index int类型,开始查找位置
         * @param target int类型,查找目标值
         * @return int类型,如果找到则返回下标值,未找到则返回-1
         */
        public static int findNumPos(int[] arr,int index,int target) {
            int ret = -1;//设置初始值,用于标记查找状态
            
            //可以考虑删除该代码段,index非人为输入,程序没有逻辑错误这段代码等于无效
            if (index < 0 || index > arr.length-1) {
                System.out.println("输入错误");
                ret = 0;
            }
            
            
            for (int i = index; i < arr.length; i++) {
                if (target == arr[i]) {
                    ret = i;
                    return ret;
                }
            }
    
            return ret;
        }
    }
    //增加两处优化:
    //1.对返回值进行判断,根据有无返回值做出不同反应
    //2.在方法中增加判断语句,确认新创建的数组长度
    public class HomeWork04261 {
        public static void main(String[] args) {
            int[] arr = {1, 3, 4, 5, 6, 7, 6, 3, 5, 7, 7, 7};//3-8
            int[] arrIndex = new int[arr.length];
            
            int find = 5;
            // 接受方法的返回值,得到的数查询目标数据的个数
            int count = findArrIndex(arr , arrIndex , find);
            
            
            //增加返回值数据判断
            if (count > 0) {
                for (int i = 0;i < count;i++) {
                    System.out.println(arrIndex[i]);
                }
            } else {
                System.out.println("Not Found");
            }
            
            
            
        }
        
        public static int findArrIndex(int[] arr , int[] arrIndex , int find) {
            
            //增加数组长度判断
            if (arr.length < arrIndex.length) {
                System.out.println("Input Parameter is Invalid");
                return 0;//不论是输入数组错误还是数组长度不对,返回0表示异常;
            }
            
            int count = 0;
            for ( int i = 0;i < arr.length; i++) {
                if (find == arr[i]) {
                    arrIndex[count] = i;
                    count++;
                }
            }
            return count;
            
        }
        
        
    }

    4.数组排序-选择排序

    import java.util.Arrays;
    public class Demo2 {
        public static void main(String[] args) {
            
            int[] arr = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };
            selectSort(arr);
            System.out.println(Arrays.toString(arr));
            
            
        }
        public static void selectSort(int[] arr) {
            for (int i = 0;i < arr.length - 1; i++) {//外层控制循环次数
                int index = i;//标记从第几个数开始参与比较
                for (int j = i + 1;j < arr.length; j++) {
                    if (arr[index] < arr[j]) {
                        index = j;
                    }
                }
                if (index != i) {
                    int temp = arr[i];
                    arr[i] = arr[index];
                    arr[index] = temp;
                }
                
            }
        }
        
    }

    5.数组排序-冒泡排序及优化方案

    //原始方案
    public class BubbleSort {
        public static void main(String[] args) {
            int[] ns = { 10, 2, 3, 4, 6, 7, 9 };
            int l = 0;
            for (int i = 0; i < ns.length - 1; i++) {
                for (int j = 0; j < ns.length - 1 - i; j++) {
                    if (ns[j] > ns[j + 1]) {
                        int temp = ns[j];
                        ns[j] = ns[j + 1];
                        ns[j + 1] = temp;
                    }
                }
                // 每次排序后打印出排序结果
                l++;
                System.out.println("外层执行" + l);
                
                for (int a = 0; a < ns.length; a++) {
                    System.out.print(ns[a] + "	");
                }
                System.out.println();
    
            }
        }
    
    }
    //加入flag判断是否提前排序OK
    public class BubbleSort2 {
        public static void main(String[] args) {
            int[] ns = { 10, 2, 3, 4, 6, 7, 9 };
            boolean flag = false;
            int l = 0;
            int n = 0;
            // int m = 0;
            for (int i = 0; i < ns.length - 1; i++) {
                flag = true;
                for (int j = 0; j < ns.length - 1 - i; j++) {
    
                    if (ns[j] > ns[j + 1]) {
                        int temp = ns[j];
                        ns[j] = ns[j + 1];
                        ns[j + 1] = temp;
                        // m=j;
                        flag = false;
                    }
    
                    n++;
                    System.out.println("内层执行" + n);
                    for (int a = 0; a < ns.length; a++) {
                        System.out.print(ns[a] + "	");
                    }
                    System.out.println();
    
                }
                l++;
                // 每次排序后打印出排序结果
                System.out.println("外层执行" + l);
                for (int a = 0; a < ns.length; a++) {
                    System.out.print(ns[a] + "	");
                }
                System.out.println();
    
                if (flag == true) {
                    break;
                }
    
            }
    
        }
    
    }
    //if 改写为 while
    public class BubbleSort3 {
        public static void main(String[] args) {
            int[] ns = { 10, 2, 3, 4, 6, 7, 9 };
            boolean flag = false;
            int l = 0;
            int n = 0;
            // int m = 0;
            int i = 0;
    
            while (i < ns.length - 1) {
    
                flag = true;
                for (int j = 0; j < ns.length - 1 - i; j++) {
    
                    if (ns[j] > ns[j + 1]) {
                        int temp = ns[j];
                        ns[j] = ns[j + 1];
                        ns[j + 1] = temp;
                        // m=j;
                        flag = false;
                    }
    
                    n++;
                    System.out.println("内层执行" + n);
                    for (int a = 0; a < ns.length; a++) {
                        System.out.print(ns[a] + "	");
                    }
                    System.out.println();
    
                }
                l++;
                // 每次排序后打印出排序结果
                System.out.println("外层执行" + l);
                for (int a = 0; a < ns.length; a++) {
                    System.out.print(ns[a] + "	");
                }
                System.out.println();
    
                if (flag == true) {
                    break;
                }
                i++;
            }
    
        }
    
    }
    //判断是否后部分序列已经排序OK 
    public class BubbleSort4 {
        public static void main(String[] args) {
    
            int[] ns = new int[] { 10, 2, 3, 4, 6, 7, 9 };
            int m = 0;// 外层循环打印计数
            int n = 0;// 内层循环打印计数
            int l = ns.length;
            int k = 0;// 用来标记最后一位需要调整位置的下标
    
            while (l > 0) {
                k = l;
                l = 0;
    
                for (int j = 0; j < k - 1; j++) {
    
                    if (ns[j] > ns[j + 1]) {
                        int temp = ns[j];
                        ns[j] = ns[j + 1];
                        ns[j + 1] = temp;
                        l = j;
    
                    }
    
                    n++;
                    System.out.println("内层执行" + n);
                    for (int a = 0; a < ns.length; a++) {
                        System.out.print(ns[a] + "	");
                    }
                    System.out.println();
    
                }
                m++;
                // 每次排序后打印出排序结果
                System.out.println("外层执行" + m);
                for (int a = 0; a < ns.length; a++) {
                    System.out.print(ns[a] + "	");
                }
                System.out.println();
    
            }
    
        }
    
    }

    6.数组逆序

    public class ArrayReverse {
    
        public static void main(String[] args) {
            int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    
            System.out.print("方法调用之前:");
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " ");
            }
            
            
            System.out.println();
            reverse(arr);
    
            
            System.out.print("方法调用之后:");
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " ");
            }
            System.out.println();
        }
    
        /**
         * 数组逆序
         *
         * @param arr 需要被逆序的int类型数组
         */
        public static void reverse(int[] arr) {
            for (int i = 0; i < arr.length / 2; i++) {//关键在这里,循环次数
                int temp = arr[i];
                arr[i] = arr[arr.length - 1 - i];
                arr[arr.length - 1 - i] = temp;
            }
        }
    }
  • 相关阅读:
    python入门-函数(二)
    python入门-函数(一)
    python入门-WHILE循环
    python入门-用户输入
    python入门-字典
    Spring Security授权 AccessDecisionManager
    Java的性能优化
    datahub
    vbs mytest
    spring发布和接收定制的事件(spring事件传播)
  • 原文地址:https://www.cnblogs.com/raising/p/12770779.html
Copyright © 2011-2022 走看看