01: sort 10 integers from small to large
package cn.bdqn.demo03; public class Demo01 { public static void main(String[] agrs){ int num[] = {12,4,3,6,8,3,9,24,56,67}; System.out.println("Order before sorting:"); for(int i=0;i<num.length;i++){ System.out.print(num[i]+" "); } System.out.println(); //Number of outer loop control lines (outline of outer loop control comparison) for(int i=0;i<num.length-1;i++){ //Number of inner loop control columns (comparison times of each round of inner loop control) for(int j=0;j<num.length-1-i;j++){ //Compare. If the previous element is larger than the following element, exchange the position if(num[j]>num[j+1]){ int temp=num[j]; num[j]=num[j+1]; num[j+1]=temp; } } } System.out.println("Order after sorting:"); for(int i=0;i<num.length;i++){ System.out.print(num[i]+" "); } System.out.println(); } }
02: find the sum of diagonal elements of a 3*3 matrix
< prompt > program analysis: use double for loop control to input a two-dimensional array, and then accumulate a[i][i] and output.
package cn.bdqn.demo03; public class Demo02 { public static void main(String[] agrs){ //Find the sum of diagonal elements of a 3*3 matrix //< prompt > program analysis: use double for loop control to input a two-dimensional array, and then accumulate a[i][i] and output. int num1[][] ={{1,2,4},{1,3,4},{6,5,3}}; int sum=0; for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ if(i==j){ sum = sum+num1[i][j]; } } } System.out.println(sum); } }
03: there is an array that has been sorted in ascending order. Now enter a number and ask to insert it into the array according to the original rule.
< tip > program analysis: first determine which element in the array this number is smaller than for the first time, then insert this number, and then move the number after this element one position later in turn.
package cn.bdqn.demo03; import java.util.Scanner; public class Demo03 { public static void main(String[] agrs){ //There is an array that has been sorted in ascending order. Now enter a number and ask to insert it into the array according to the original rule. //< tip > program analysis: first determine which element in the array this number is smaller than for the first time, then insert this number, and then move the number after this element one position later in turn. //Method 1: int []arr1 = {10,20,30,40,52,60}; int []arr2 = new int[arr1.length+1]; Scanner sc = new Scanner(System.in); System.out.println("Please enter an integer"); int num = sc.nextInt(); int n = 0 ; for(int i = 0;i<arr1.length;i++){//Find the position of the input array in the array if(arr1[i]>num) { n=i; break; }else{ n=arr1.length; } } for(int j = 0;j<arr2.length;j++){//Assign value to array 2 and output if(j<n){ arr2[j] = arr1[j]; }if(j==n){ arr2[j] = num; }if(j>n){ arr2[j] = arr1[j-1]; } System.out.print(arr2[j]+" "); } //Method 2: int num1[] = {11,22,33,44,55,66}; int num2[]=new int[num1.length+1]; for(int i=0;i<num1.length;i++){ num2[i]=num1[i]; } Scanner sc = new Scanner(System.in); System.out.println("Please enter a number:"); int insert = sc.nextInt(); int index= num2.length-1; for(int i=0;i<num2.length;i++){ if(insert<num2[i]){ index=i; break; } } for(int i=num2.length-1;i>index;i--){ num2[i]=num2[i-1]; } num2[index]=insert; for(int i=0;i<num2.length;i++){ System.out.print(num2[i]+" "); } } }
04: output an array in reverse order.
package cn.bdqn.demo03; import java.util.Scanner; public class Demo04 { public static void main(String[] agrs){ //Output an array in reverse order. int[] s = {90,80,60,95,40,65,20}; System.out.println("Order before sorting:"); for(int i=0;i<s.length;i++){ System.out.print(s[i]+" "); } System.out.println(); System.out.println("Order after sorting:"); for(int i =6;i>=0;i--){ System.out.print(s[i]+" "); } } }
05: input the array, exchange the largest with the first element, and the smallest with the last element, and output the array.
package cn.bdqn.demo03; import java.util.Arrays; import java.util.Scanner; public class Demo05 { public static void main(String[] agrs){ //Input array, the largest one is exchanged with the first element, and the smallest one is exchanged with the last element, and output array. int[] a =new int[10]; Scanner sc = new Scanner(System.in); for(int i=0;i<a.length;i++){ System.out.println(" " + (i + 1) ); a[i]=sc.nextInt(); } System.out.println(" "+Arrays.toString(a)); int max = a[0]; int min = a[0]; int maxIndex =0; int minIndex=0; for(int i = 1;i<a.length;i++){ //Find the subscript of the maximum value and the minimum value if(a[i]>max){ max=a[i]; maxIndex=i; }if(a[i]<min){ min=a[i]; minIndex=i; } } //Exchange the maximum value with the first element, and the minimum value with the last element int temp=a[maxIndex]; a[maxIndex]=a[0]; a[0]=temp; int temp1=a[minIndex]; a[minIndex]=a[a.length-1]; a[a.length-1]=temp1; System.out.println(); for(int i=0;i<a.length;i++){ System.out.print(a[i]+" "); } } }
06: there are n integers, so that the previous numbers are moved backward by m positions, and the last m numbers become the first m numbers
package cn.bdqn.demo03; import java.util.Scanner; public class Demo06 { public static void main(String[] agrs){ //There are n integers, so that the previous numbers are moved backward by m positions, and the last m numbers become the first m numbers int nums[] = {11,22,33,44,55,66,77,88}; for (int i : nums) { System.out.print(i+" "); }System.out.println(); Scanner sc = new Scanner( System.in); //Get the number to move from the keyboard System.out.println("Please enter the number you want to move:"); int num = sc.nextInt(); int nums1[] = new int[num]; //Place the m bits in the new array after moving for(int i=nums.length-num; i< nums.length;i++){ nums1[i- (nums.length-num)] = nums[i]; } //Move the previous number by m bits for(int i = nums.length-1-num; i>=0;i--){ nums[i+num]=nums[i]; } //The last m bits are assigned to the original array for(int i=0; i< nums1.length;i++){ nums[i] = nums1[i]; } //Array after output operation for (int i : nums) { System.out.print(i+" "); } } }
07: define a two-dimensional array with four rows and four columns, input values one by one from the keyboard, and then exchange the data of row 1 and row 4, and the data of row 2 and row 3
package cn.bdqn.demo03; import java.util.Scanner; public class Demo07 { public static void main(String[] agrs){ //Define a two-dimensional array with four rows and four columns, input values one by one from the keyboard, and then exchange the data of row 1 and row 4, //Exchange the data of lines 2 and 3 int a[][]=new int[4][4]; Scanner sc = new Scanner(System.in); for(int i=0;i<a.length;i++){ System.out.println("Section"+(i+1)+"that 's ok"); for(int j=0;j<a[i].length;j++){ System.out.println("Please enter No" + (j + 1)+"number" ); a[i][j]=sc.nextInt(); } } //The two-dimensional array matrix you entered System.out.println("The matrix you entered is:"); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j]+" "); } System.out.println(); } //Number of exchange rows int[] temp = new int[4]; for (int i = 0; i < a.length; i++) { temp[i] = a[0][i]; a[0][i] = a[3][i]; a[3][i] = temp[i]; temp[i] = a[1][i]; a[1][i] = a[2][i]; a[2][i] = temp[i]; } //Output post exchange matrix System.out.println("The matrix after exchange is:"); for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ System.out.print(a[i][j]+" "); } System.out.println(); } } }
08: define a two-dimensional array with 3 rows and 4 columns, input values one by one from the keyboard, and write a program to clear the surrounding data to 0
package cn.bdqn.demo03; import java.util.Scanner; public class Demo08 { public static void main(String[] agrs){ //Define a two-dimensional array with 3 rows and 4 columns, input values one by one from the keyboard, and write a program to clear the surrounding data to 0 int a[][]=new int[3][4]; Scanner sc = new Scanner(System.in); for(int i=0;i<a.length;i++){ System.out.println("Section"+(i+1)+"that 's ok"); for(int j=0;j<a[i].length;j++){ System.out.println("Please enter No" + (j + 1)+"number" ); a[i][j]=sc.nextInt(); } } // Traverse before clearing 0 System.out.println("Traverse before clearing 0:"); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j] + "\t"); } System.out.println(); } // Clear around 0 for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { if (i == 0 || i == a.length - 1) { a[i][j] = 0; } else if (j == 0 || j == a[i].length - 1) { a[i][j] = 0; } } } // Traverse after clearing 0 System.out.println("Traverse after clearing 0:"); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j] + "\t"); } System.out.println(); } } }
09: define a two-dimensional array with 3 rows and 4 columns, input values one by one from the keyboard, and clear the value of the lower left triangle to 0
package cn.bdqn.demo03; import java.util.Scanner; public class Demo09 { public static void main(String[] agrs){ //Define a two-dimensional array with 3 rows and 4 columns, input values one by one from the keyboard, and clear the values of the lower left triangle to 0 int a[][]=new int[3][4]; Scanner sc = new Scanner(System.in); for(int i=0;i<a.length;i++){ System.out.println("Section"+(i+1)+"that 's ok"); for(int j=0;j<a[i].length;j++){ System.out.println("Please enter No" + (j + 1)+"number" ); a[i][j]=sc.nextInt(); } } // Traverse before clearing 0 System.out.println("Traverse before clearing 0:"); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j] + "\t"); } System.out.println(); } //Clear the value of the lower left triangle to 0 for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { if(i == 0 && j == 0){ a[i][j] = 0; //[a[1].length - 2] if i=1, it means a[1].length array minus a[0] and a[1]. }else if(i == 1 && j < a[i].length - 2){ a[i][j] = 0; }else if(i == a.length - 1 && j < a[i].length - 1){ a[i][j] = 0; } } } // Traverse after clearing 0 System.out.println("Traverse after clearing 0:"); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j] + "\t"); } System.out.println(); } } }
10: Define a two-dimensional array with 4 rows and 4 columns, input values one by one from the keyboard, and clear the diagonal value to 0
package cn.bdqn.demo03; import java.util.Scanner; public class Demo10 { public static void main(String[] agrs){ //Define a two-dimensional array with 4 rows and 4 columns, input values one by one from the keyboard, and clear the diagonal value to 0 int a[][]=new int[4][4]; Scanner sc = new Scanner(System.in); for(int i=0;i<a.length;i++){ System.out.println("Section"+(i+1)+"that 's ok"); for(int j=0;j<a[i].length;j++){ System.out.println("Please enter No" + (j + 1)+"number" ); a[i][j]=sc.nextInt(); } } // Traverse before clearing 0 System.out.println("Traverse before clearing 0:"); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j] + "\t"); } System.out.println(); } //Clear the diagonal value to 0 for (int i = 0; i <a.length ; i++){ for (int j = 0; j <a[i].length; j++){ if (i == j){ a[i][j] = 0; continue; } } } // Traverse after clearing 0 System.out.println("Traverse after clearing 0:"); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j] + "\t"); } System.out.println(); } } }
11: Define an N*N two-dimensional array, input values from the keyboard, find out the maximum value in each line to form a one-dimensional array and output it;
package cn.bdqn.demo03; import java.util.Arrays; import java.util.Scanner; public class Demo11 { public static void main(String[] agrs){ //Define an N*N two-dimensional array, input values from the keyboard, find out the maximum value in each line to form a one-dimensional array and output it; int a[][]=new int[3][4]; Scanner sc = new Scanner(System.in); for(int i=0;i<a.length;i++){ System.out.println("Section"+(i+1)+"that 's ok"); for(int j=0;j<a[i].length;j++){ System.out.println("Please enter No" + (j + 1)+"number" ); a[i][j]=sc.nextInt(); } } // Array obtained from keyboard System.out.println("Traverse before clearing 0:"); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j] + "\t"); } System.out.println(); } //Find the maximum value in each row to form a one-dimensional array and output it int[] arr = new int[a.length]; for (int i = 0; i < a.length ; i++){ int max = a[i][0]; for (int j = 1; j < a[i].length; j++){ if (a[i][j] > max){ max = a[i][j]; } } arr[i] = max; } System.out.println(Arrays.toString(arr)); // Output array System.out.println("The maximum values in each row form a one-dimensional array:"); for (int i = 0; i < a.length; i++) for (int j = 0; j < a[i].length; j++) { System.out.print(a[i][j] + "\t"); } System.out.println(); } }
12: Input a 3*4 integer array from the keyboard, calculate the maximum and minimum values, and display
package cn.bdqn.demo03; import java.util.Arrays; import java.util.Scanner; public class Demo12 { public static void main(String[] agrs){ //Input a 3*4 integer array from the keyboard, calculate the maximum and minimum values, and display int[][] a =new int[3][4]; Scanner sc = new Scanner(System.in); for(int i=0;i<a.length;i++){ System.out.println("Section"+(i+1)+"that 's ok"); for(int j=0;j<a[i].length;j++){ System.out.println("Please enter No" + (j + 1)+"number" ); a[i][j]=sc.nextInt(); } } System.out.println("The array you entered is:"); System.out.println(); int max,min; //initialization max=a[0][0]; min=a[0][0]; for(int i=0;i<3;i++) { for(int j=0;j<4;j++) { if(a[i][j]>max) { max=a[i][j]; } else if(a[i][j]<min) { min=a[i][j]; } } } System.out.print(" The minimum value is:"+min); System.out.print("Maximum,"+max); } }
13: There are 10 numbers required to be output from large to small with the selection method and the bubbling method respectively
package cn.bdqn.demo03; import java.util.Scanner; public class Demo13 { public static void main(String[] numgrs){ //There are 10 numbers required to be output from large to small with the selection method and the bubbling method respectively // Select sort int[] nums = { 11, 56, 94, 78, 33, 32, 55, 67, 86, 13 }; // Output array before sorting System.out.println("Output array before sorting"); for (int i = 0; i < nums.length; i++){ System.out.print(nums[i] + " "); } System.out.println(); for (int i = 0; i < nums.length - 1; i++){ int max = i; for (int j = i + 1; j < nums.length; j++){ if (nums[j] > nums[max]){ // Record the subscript of the maximum value element that can be found so far max = j; } } if (i != max){//When max is not equal to the maximum value of i, the subscript is the first default and the first exchange int tmp = nums[i]; nums[i] = nums[max]; nums[max] = tmp; } } // Output array after sorting System.out.println("Output array after sorting"); for (int i = 0; i < nums.length; i++){ System.out.print(nums[i] + " "); } // Bubble sort int[] nums = { 11, 56, 94, 78, 33, 32, 55, 67, 86, 13 }; // Output array before sorting System.out.println("Output array before sorting"); for (int i = 0; i < nums.length; i++){ System.out.print(nums[i] + " "); } System.out.println(); for (int i = 0; i < nums.length - 1; i++){ for (int j = 0; j < nums.length - i - 1; j++){ if (nums[j] < nums[j + 1]){ int temp = nums[j]; nums[j] = nums[j + 1]; nums[j + 1] = temp; } } } System.out.println("Output array after sorting"); for (int i = 0; i < nums.length; i++){ System.out.print(nums[i] + " "); } } }