The case of two-dimensional array, binary search method, Yang Hui triangle in Java array

1. Code and renderings

1. Two-dimensional array case and effect diagram

Case: It is known that there are 5 students in each of 3 classes. Please use a two-dimensional array to dynamically enter the results of all students, and calculate the average, best and worst results of each class. Output in turn, the class with the best average score, the highest score and the worst score among all students in the three classes.

The code is as follows (example):

public class Work3 {
    
    
    public static void main(String[] args) {
    
    
        double [][] scores=new double[3][5];
        double [] avg=new double[3];//平均成绩
        double [] max=new double[3];//最好成绩
        double [] min=new double[3];//最差成绩
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < scores.length; i++) {
    
    
            double sum=0;
            double maxInit=-1;
            double minInit=1000;
            for (int j = 0; j < scores[i].length; j++) {
    
    
                //成绩录入
                System.out.println("请输入第"+(i+1)+"个班的第"+(j+1)+"个学员的成绩:");
                scores[i][j]=sc.nextDouble();
                sum+=scores[i][j];//求和
                //求每个班最大值
                if(scores[i][j]>maxInit){
    
    
                    maxInit=scores[i][j];
                }
                //求每个班最小值
                if(scores[i][j]<minInit){
    
    
                    minInit=scores[i][j];
                }
            }
            avg[i]=sum/5;
            max[i]=maxInit;
            min[i]=minInit;
        }

        //求出平均成绩最好的班
        double Max=avg[0];
        int MaxIndex=0;
        for (int i = 1; i < avg.length; i++) {
    
    
            if(avg[i]>Max){
    
    
                Max=avg[i];
                MaxIndex=i;
            }
        }
        System.out.println("平均成绩最好的班是"+(MaxIndex+1)+"班,分数是"+Max);
        //所有学员中分数最高和最差
        Arrays.sort(max);
        Arrays.sort(min);
        System.out.println(max[2]);
        System.out.println(min[0]);
    }
}

Insert picture description here

2. Binary search method and renderings

The code is as follows (example):
Case: Use the Arrays class to sort the array {1,5,12,36,55,78,98} in ascending order, ask the user to enter a number to be searched, determine whether the number exists, and if it exists, output it If the position in the array does not exist after ascending order, a prompt message will be output.

public class Work4 {
    
    
    public static void main(String[] args) {
    
    
    int[] arr={
    
    1,5,12,36,55,78,98};
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个要查找的数字:");
        int X=sc.nextInt();
        fun(arr,X);
    }

    private static void fun(int[] arr,int X) {
    
    
        int start=0,end=arr.length-1,mid=(start+end)/2;
        while (start<=end){
    
    
            if (X==arr[mid]){
    
    
                System.out.println("找到了,下标为:"+mid);
                return;
            }else if (X>arr[mid]){
    
    
                start=mid+1;
            }else {
    
    
                end=mid-1;
            }
            mid=(start+end)/2;
        }
        System.out.println("找不到");
    }
}

Insert picture description here
Insert picture description here

3. Report the number and select the monitor and the effect picture

The code is as follows (example):
Case: Election of the class leader according to certain rules, the rules are as follows: circle N people in the class, number them in sequence, start from the first person to report the number (from 1 to 3), wherever you report 3 people withdraw from the circle, and then start again from the next person, the last person left is selected as the monitor. According to this rule, the selected monitor is the student with the original number, assuming that there are 25 students in the class. personal

public class Work01 {
    
    
    public static void main(String[] args) {
    
    
        int [] stus=new int[25];
        //编号
        for (int i = 0; i < stus.length; i++) {
    
    
            stus[i]=i+1;
        }
        //标记法,用0表示退出
        int k=0;//报数器
        int num=0;//表示淘汰的人数
        while (true){
    
    //重复转圈报数
            //表示实现一轮报数
            for (int i = 0; i < stus.length; i++) {
    
    
                if (stus[i]!=0){
    
    //过滤掉淘汰的人
                    k++;//报数
                    if (k%3==0){
    
    
                        stus[i]=0;//淘汰
                        num++;
                        if (num==24){
    
    //淘汰人数为24,结束游戏
                            System.out.println(Arrays.toString(stus));
                            return;
                        }
                    }
                }
            }
        }
    }
}

Insert picture description here

4. Yang Hui triangle and its renderings

The code is as follows (example):
Case: 2. Print Yang Hui triangle (required to print 10 lines), as follows: (using a two-dimensional array)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

package work1;

public class Work02 {
    
    
    public static void main(String[] args) {
    
    
        //定义一个长度为10的二维数组
        int [][] num=new int[10][];
        //行数和个数的关系
        for (int i = 0; i <num.length ; i++) {
    
    
            num[i]=new int[i+1];
        }
        for (int i = 0; i < num.length; i++) {
    
    
            for (int j = 0; j < num[i].length; j++) {
    
    
                if(j==0||j==num[i].length-1){
    
    
                    num[i][j]=1;//左右端点
                }else {
    
    
                    num[i][j]=num[i-1][j-1]+num[i-1][j];
                }
            }
        }
        //输出
        for (int i = 0; i < num.length; i++) {
    
    
            //输出每行前面的空格
            for (int j = 0; j < 10-i; j++) {
    
    
                System.out.print("  ");
            }
            for (int j = 0; j < num[i].length; j++) {
    
    
                if(num[i][j]>=100)
                    System.out.print(num[i][j]+" ");
                else if(num[i][j]>=10)
                    System.out.print(num[i][j]+"  ");
                else
                    System.out.print(num[i][j]+"   ");
            }
            System.out.println();
        }
    }
}

Insert picture description here

to sum up

The above is the case content related to the two-dimensional array and the binary search method, which is mainly realized by using the characteristics of the two-dimensional array and the combination of loops.

Guess you like

Origin blog.csdn.net/StruggleBamboo/article/details/110877057