Home  >  Article  >  Java  >  Java uses a two-dimensional array to print a 10-line Yang Hui triangle

Java uses a two-dimensional array to print a 10-line Yang Hui triangle

王林
王林forward
2023-04-19 22:40:051508browse

Sum of elements in an array

public class T02 {
    public static void main(String[] args) {
        int[][]arr=new int[][]{{1,2,3,4,5},{1,2,3,5},{8,9,7}};
        int sum=0;
        for(int i=0;i< arr.length;i++){
            for(int j=0;j<arr[i].length;j++){
               sum=arr[i][j]+sum;
            }
        }
        System.out.println("sum="+sum);
    }
}
//和为50

Use a two-dimensional array to print a 10-line Yang Hui triangle

public class T02 {
    public static void main(String[] args) {
        //声明并且初始化数组
        int[][]arr=new int[10][];
        //给数组的元素赋值
        for(int i=0;i< arr.length;i++){
            arr[i]=new int[i+1];
            arr[i][0]=arr[i][i]=1;
            if(i>1){
                for(int k=1;k<arr[i].length-1;k++){
                    arr[i][k]=arr[i-1][k-1]+arr[i-1][k];
                }
            }
        }
        //遍历数组
        for(int i=0;i< arr.length;i++){
            for(int j=0;j<arr[i].length;j++){
                System.out.print(arr[i][j]+"\t");
            }
            System.out.println();
        }
    }
}

Find the maximum, minimum, average, and Sum, etc.

public class T03 {
    public static void main(String[] args) {
        int[] arr=new int[10];
        for(int i=0;i< arr.length;i++){
            arr[i]=(int)Math.random()*((99-10+1)+10);
            //[a,b]中的随机数的公式:Math.readom()*((b-a+1)+a).
            //注意这里出来的为double类型。
        }
        //求最大值
        int maximum=0;
        for(int i=0;i< arr.length;i++){
            if(maximum<arr[i]){
                maximum=arr[i];
            }
        }
        System.out.println("最大值为:"+maximum);
        //求最小值
        int minimum=arr[0];
        for(int i=1;i< arr.length;i++){
            if(minimum>arr[i]){
                minimum=arr[i];
            }
        }
        System.out.println("最大值为:"+minimum);
        //求和
        int sum=0;
        for(int i=1;i< arr.length;i++){
            sum=sum+arr[i];
        }
        System.out.println("sum:"+sum);
        //求平均数
        System.out.println("平均数为:"+sum/ arr.length);
    }
}

*Use a simple array

(1)Create a class named T04 and declare two variables array1 and array2 in the main() method

They It is an array of type int[].

(2) Use curly brackets {} to initialize array1 to 8 prime numbers: 2, 3, 5, 7, 11, 13, 17, 19.

(3) Display the contents of array1.

(4) Assign array2 variable equal to array1, modify the even index element in array2 to make it equal to the index value (such as array[0]=0, array[2]=2). Print out array1. **Thinking: What is the relationship between array1 and array2?
Extension: Modify the question to realize the copying of array1 array by array2

public class T04 {
    public static void main(String[] args) {
        int[] array1,array2;
        array1=new int[]{2,3,5,7,11,13,17,19};
        for(int i=0;i< array1.length;i++){
            System.out.print(array1[i]+"\t");
        }     //赋值array1变量等于array2     //不能称作数组的复制
        array2=array1;
        for(int i=0;i< array1.length;i++){
            if(i%2==0){
                array2[i]=i;
            }
        }
        System.out.println();
        System.out.println("******************************************");
        for(int i=0;i< array1.length;i++){
            System.out.print(array1[i]+"\t");
        }
    }
}

Java uses a two-dimensional array to print a 10-line Yang Hui triangle

(1) The addresses of array1 and array2 The values ​​are the same, pointing to the only array entity in the heap space

(2)

 for(int i=0;i< array1.length;i++){
            array2[i]=array1[i];
        }

Java uses a two-dimensional array to print a 10-line Yang Hui triangle

Method 2

int i=0;
        int j=0;
        for(i=0,j= arr.length-1;i<j;i++,j--){
            int a=arr[i];
            arr[i]=arr[j];
            arr[j]=a;
        }

Linear search

public class T05 {
    public static void main(String[] args) {
        int[]arr=new int[]{1,2,3,4,5,6,7,8,9};
        for(int i=0;i< arr.length;i++){
            System.out.print(arr[i]+"\t");
        }
        System.out.println();
        for(int i=0;i< arr.length;i++){
            if(i< arr.length-1-i){
                int a=arr[i];
                arr[i]=arr[arr.length-1-i];
                arr[arr.length-1-i]=a;
            }
        }
        for(int i=0;i< arr.length;i++){
            System.out.print(arr[i]+"\t");
        }
    }
}

Java uses a two-dimensional array to print a 10-line Yang Hui triangle

Java uses a two-dimensional array to print a 10-line Yang Hui triangle

##Binary search

//The premise must be in order

Example : Let’s first look at

import java.util.Scanner;

public class T07 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int a= s.nextInt();
        int[]arr=new int[]{1,2,3,4,6,7,8,9,10};
        int left=0;
        int right= arr.length-1;
        boolean is=true;
        while(left<=right){
            int average=(int)(left+right)/2;
            if(arr[average]>a){
                right=average-1;
            } else if(a==arr[average]){
                System.out.println("找到了,下标是:"+average);
                is=false;
            } else {
                left = average + 1;
//            }if(left==right){
//                System.out.println("没有找到");
//                is=false;
            }
        }
        if(is){
            System.out.println("很遗憾没有找到");
        }
    }
}

bubble sorting from small to large

public class T08 {
    public static void main(String[] args) {
        int[]arr=new int[]{33,55,2,6,-8,-5,66,1,63};
        for(int i=0;i< arr.length-1;i++){
            for(int j=0;j< arr.length-1-i;j++){
                if(arr[j]>arr[j+1]){
                    int a=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=a;
                }
            }
        }
        for(int i=0;i< arr.length;i++) {
                System.out.println(arr[i]);
        }
    }
}

Java uses a two-dimensional array to print a 10-line Yang Hui triangle## Find the sum of the diagonal elements of a 3*3 matrix

This is a matrix programming implementation question. Matrices in Java are generally implemented through two-dimensional arrays.

The specific code is as follows:

import java.util.Random;

/**
 * 求一个3*3矩阵对角线元素之和
 *
 * @author ChenZX
 *
 */
public class Test04 {

    public static void main(String[] args) {
        int sum = 0; //和
        int[][] arr = new int[3][3];
        Random r = new Random();
        for(int i=0;i<3;i++){    //随机生成矩阵
            for(int j=0;j<3;j++){
                arr[i][j] = r.nextInt(10);  //0到9
            }
        }
        for(int i=0;i<3;i++){      //遍历矩阵
            for(int j=0;j<3;j++){
                System.out.print(arr[i][j]+" ");   //打印矩阵元素
                if(i==j){   //如果为对角线元素
                    sum += arr[i][j];  //求和
                }
            }
            System.out.println(); //每输出3个元素换行
        }
        System.out.println("此矩阵对角线的和为:"+sum);
    }
}

The above is the detailed content of Java uses a two-dimensional array to print a 10-line Yang Hui triangle. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete