Java - Daily Programming Exercises II (Array Exercises)

Keywords: Java less Programming

1. There is an array that has been sorted. Now enter a number and ask to insert it into the array according to the original rule.

 

Analytical thinking:

The Random class creates an array at random, then calls the sort method in the Array class to sort the array, and then starts to implement the function.

Inserting arrays according to the original rules is very simple, as long as the appropriate insertion position n is found, then the array elements before n are copied directly to the corresponding position of the new array, and the input number is inserted at n, then the elements after n are moved one lattice and then moved to the new array.

The key is to find the right insertion position. The search algorithm is obviously better than binary search. Here I throw in a lazy, direct use of binary Search () in the Array class to find the input num. If there is the same number as num in the array, then the return value of the method is the subscript position of the number, then the insertion position n is the return value; and if num does not exist in the array, the method can return a negative number, num is smaller than the first element of the array. In this case, the insertion position should be 0, greater than the first element and less than the second element is - 2, and then the insertion position should be 1, and so on... Obviously, the insertion position should be subtracted by one more absolute value for the return value.

Finding a location is easy to implement.

 1 import java.util.Arrays;
 2 import java.util.Random;
 3 import java.util.Scanner;
 4 
 5 /**
 6  * There is a sorted array. Now enter a number and ask to insert it into the array according to the original rule.
 7  * 
 8  * @author ChenZX
 9  *
10  */
11 public class Test30 {
12 
13     public static void main(String[] args) {
14         Random r = new Random();
15         int[] arr = new int[10];
16         for(int i=0;i<10;i++){    //Random generated array
17             arr[i]= r.nextInt(100);
18         }
19         System.out.println(Arrays.toString(arr));
20         Arrays.sort(arr);   //sort
21         System.out.println(Arrays.toString(arr));
22         
23         Scanner s = new Scanner(System.in);
24         System.out.println("Please enter a number:");
25         int num = s.nextInt();
26         s.close();
27         int bs =Arrays.binarySearch(arr, num); //The results of binary search are stored in bs
28         int n = 0; //Insertion position
29         if(bs<0){             //Search failed,The insertion position is bs Absolute value-1
30             n = Math.abs(bs)-1; 
31         }else{        //Search success,The insertion position is bs
32             n = bs;
33         }
34         System.out.println(bs);
35         
36         int[] arr2 = new int[arr.length+1]; //Create a new array with the length of the original array+1
37         for(int i=0;i<arr2.length;i++){  
38             if(i<n){            //Just copy the part directly before insertion
39                 arr2[i]=arr[i];
40             }else if(i==n){   //Insert the input number into the corresponding position
41                 arr2[i]=num;
42             }else{               //The number after the insertion position moves back one grid
43                 arr2[i]=arr[i-1];
44             }
45         }
46         System.out.println("The array after insertion is:");
47         System.out.println(Arrays.toString(arr2));
48     }
49 }

 

2. Find the sum of diagonal elements of a 3*3 matrix

 

Analytical thinking:

This is a matrix programming problem. In Java, matrices are usually implemented through two-dimensional arrays.

The code is as follows:

 1 import java.util.Random;
 2 
 3 /**
 4  * Finding the Sum of Diagonal Elements of a 3*3 Matrix
 5  * 
 6  * @author ChenZX
 7  *
 8  */
 9 public class Test29 {
10 
11     public static void main(String[] args) {
12         int sum = 0; //and 
13         int[][] arr = new int[3][3];
14         Random r = new Random();
15         for(int i=0;i<3;i++){    //Random Generation Matrix
16             for(int j=0;j<3;j++){
17                 arr[i][j] = r.nextInt(10);  //0 To 9
18             }
19         }
20         for(int i=0;i<3;i++){      //ergodic matrix
21             for(int j=0;j<3;j++){
22                 System.out.print(arr[i][j]+" ");   //Print matrix elements
23                 if(i==j){   //If it's a diagonal element
24                     sum += arr[i][j];  //Summation
25                 }
26             }
27             System.out.println(); //Line wrapping for each output of three elements
28         }
29         System.out.println("The sum of the diagonals of this matrix is:"+sum);
30     }
31 }

Posted by jmack159 on Thu, 25 Apr 2019 12:03:35 -0700