Introduction of today's content
1. Cycle Practice
2. Array method Exercises
01 Odd Sum Exercises
* A: 奇数求和练习 * a: 题目分析 * 为了记录累加和的值,我们需要定义一个存储累加和的变量 * 我们要获取到1-100范围内的数 * 判断当前数是否为奇数,是奇数,完成累加和操作 * 累加完毕后,最终显示下累加和的值 * b: 解题步骤 * 定义一个用来记录累加和的变量 * 使用for循环语句,完成1-100之间每个数的获取 * 使用if条件语句,判断当前数是否是奇数,是奇数,进行累加和操作 * 使用输出语句,打印累加和变量的值 * c: 案例代码 public class Test01 { public static void main(String[] args) { int sum = 0; for (int i = 0; i < 100; i++) { if (i%2==1) { sum += i; } } System.out.println("累加和的值 " + sum); } }
02 Narcissus exercise Function Realization
* A: 水仙花练习功能实现 * a: 题目分析 * 明确什么样的数就是水仙花数。水仙花数是指一个3位数(100-999之间),其每位数字立方之和等于该3位数本身。 如153 = 1*1*1 + 3*3*3 + 5*5*5,即 3位数本身 = 百位数立方 + 十位数立方 + 个位数立方; * 获取水仙花范围内的所有3位数(100-999之间的每个3位数) * 判断该3位数是否满足水仙花数,满足,打印该3位数 * b: 解题步骤 * 使用for循环,得到100-999之间的每个3位数 * 获取3位数中百位数字、十位数字、个位数字 * 使用if条件语句,判断该3位数是否满足水仙花数,满足,使用输出语句,打印该3位数 * c: 案例代码 public class Test02 { public static void main(String[] args) { for (int i = 100; i < 1000; i++) { int bai = i/100%10; int shi = i/10%10; int ge = i%10; if (i == bai*bai*bai + shi*shi*shi + ge*ge*ge) { System.out.println(i); } } } }
03ASCII Encoding Table
* A: ASCII编码表 * a: 英文全称 * American Standard Code for Information Interchange,美国标准信息交换代码 * b: ASCII编码表由来 * 计算机中,所有的数据在存储和运算时都要使用二进制数表示 * a、b、c、d这样的52个字母(包括大写)、以及0、1等数字还有一些常用的符号, 在计算机中存储时也要使用二进制数来表示 * 具体用哪些二进制数字表示哪个符号,当然每个人都可以约定自己的一套(这就叫编码) * 大家如果要想互相通信而不造成混乱,那么大家就必须使用相同的编码规则,于是美国有关的标准化组织就出台了ASCII编码, 统一规定了上述常用符号用哪些二进制数来表示。 * c: 中文编码表 * GB2312 * UNICODE * d: 字符中重要的ASCII码对应关系 * a : 97 * A : 65 * 0 : 48
04char type of storage
* A:char type of storage * A: Range of Values * Short: Occupies two bytes, is signed data, range of values -32768-32767 * Char: Two bytes, is unsigned data, value range 0-65536 * B: Type conversion * Char type data to line Regulation regulation int data type * C: Case code/* ASCII code show character Java data type , char integer java data type, int int type and char data type convert char two bytes, int four bytes When char is converted to int type, the type automatically prompts, char data type, will query the encoding table, get integer int into char type, cast, will query the encoding table Char store kanji, Query Unicode encoding table char can be computed with int, prompt for int type, two bytes char in memory range is 0-65535, unsigned data type */ public class asciidemo{public static void Main (string[] args) {char c = ' a '; int i = c + 1; System.out.println (i); int j = 90; Char h = (char) j; System.out.println (h); System.out.println ((char) 6); Char k = ' You'; System.out.println (k); char m =-1; } }
05 Output All English letters
* A: 输出所有英文字母 * a: 题目分析 * 一共26个大小写字母,那么,可以考虑循环26次。在每次循环中,完成指定字母的大小写打印 * 找出ABCDEFG…XYZ这些字母之间的变化规律 通过ASCII表发现,后面的字母比它前面的字母,ASCII值大1 下一个字母 = 上一个字母 + 1 如: A B C D 65 66 67 68 * 在每次循环中打印上一个字母大小写,并指定下一个字母 * b: 解题步骤 * 定义初始化大写变量,值为’A’; 初始化小写变量,值为’a’ * 使用for循环,进行26次循环 * 在每次循环中,打印大写字母、小写字母。 每次打印完成后,更新大写字母值、小写字母值 * c: 案例代码 public class Test04 { public static void main(String[] args) { char da = 'A'; char xiao = 'a'; for (int i = 0; i < 26; i++) { System.out.println("大写字母 "+da+" ,小写字母 "+xiao); da++; //更新大写字母值 xiao++; //更新小写字母值 } } }
0699 analysis of the multiplication table
* A: 99乘法表的分析 * a: 打印格式 1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 * b: 题目分析 通过观察发现,如果把1*1=1这样的内容 看做一颗*的话,那么打印结果就成了如下效果: * ** *** **** … 这样,就是打印9行星,每行打印星的个数与当前行数相等。 再观察“1*3=3 2*3=6 3*3=9”得出它们如下的变化规律: 每行第n次 +"*"+ 行号 +"="+ 每行第n次 * 行号 如: 1 +"*"+ 2 +"="+ 1*2; // 相当于1*2=2 2 +"*"+ 2 +"="+ 2*2; // 相当于2*2=4 * c: 解题步骤 * 定义一个外层for循环,初始值从1开始,循环9次。用来控制打印的行数 * 在外层for循环内部,定义一个for循环,初始值从1开始,循环次数与当前行数相等。用来完成每行打印指定次数的乘法公式 如1*1=1 * 在内层for循环中,完成每行指定次数的乘法公式打印 如1*1=1 System.out.print(k +"*"+ j +"="+ j*k +"\t"); // 变量k代表:每行中的第n次 // 变量j代表:行号 * 在外循环中,当每行指定次数的乘法公式打印完毕后,通过System.out.println()切换到下一行。 这样,再次打印乘法公式时,就在下一行输出打印了
0799 function Realization of multiplication table
* A: 99乘法表的功能实现 * a: 案例代码 /* 利用嵌套for循环,实现99乘法表示 实现步骤: 1. 定义外循环控制行数 2. 内循环控制个数,个数,每次都在递增 3. 循环中输出,乘法表的格式 1*3=3 */ public class Test05 { public static void main(String[] args) { for (int j = 1; j < 10; j++) { for (int k = 1; k <= j; k++) { System.out.print(k +"*"+ j +"="+ j*k +"\t"); } System.out.println(); } } }
08 implementing the traversal of an array
* A: Implementing the traversal of an array * A: Topic analysis * Through loops, we can complete the acquisition of elements in the array, the array name [index] * observe that a comma "," is added between each array element, and that there is a pair of brackets "[]" wrapped around the entire array Array of all elements. * B: Problem Solving steps * Use the output statement to finish printing the left square brackets "[" * use loops to output array element values. The output element value is divided into two cases, as follows: * The last array element, plus a right-hand bracket "]" * not the last array element, plus a comma "," * C: Case Code/* Defines the method that implements the traversal traversal of the array, output [11,33,565,66,78,89] int[] arr = {3,4,45,7}; The result contains a string, [], the implementation step: 1. Defines a method that implements the traversal of an array of 2. First, print [brackets 3. Iterate over the elements of the array output array and comma to determine if the last element of the array is traversed, if it is the last element, the output] bracket */ public class arraymethodtest{public static void Main (string[] args) {int[] arr = {11,44,55,33, 66}; PrintArray (arr); Int[] arr2 = {22,88,99,33,66}; PrintArray (ARR2); }/* Define method, implement function return value: Void method parameter:Array */public static void PrintArray (int[] arr) {//output half brackets, do not wrap print Syste M.out.print ("["); The array is traversed for (int i = 0; i < arr.length; i++) {//determines which element is traversed, is not the last element of the array How to determine the loop variable to reach length-1 if (i = = arr.length-1) {//output array elements and] System.out.print (arr[i]+ "]"); }else{//is not the last element of the array, the output array element and the comma System.out.print (arr[i]+ ","); }} System.out.println (); } }
09 Inverse Array principle
* A: 数组逆序原理 * a: 题目分析(图解见day07_source/数组的逆序原理.JPG) * 通过观察发现,本题目要实现原数组元素倒序存放操作。即原数组存储元素为{11,22,33,44}, 逆序后为原数组存储元素变为{44,33,22,11}。 * 通过图解发现,想完成数组元素逆序,其实就是把数组中索引为start与end的元素进行互换。 * 每次互换后,start索引位置后移,end索引位置前移,再进行互换 * 直到start位置超越了end位置,互换结束,此时,数组元素逆序完成。 * b: 解题步骤 * 定义两个索引变量start值为0,变量end值为数组长度减去1(即数组最后一个元素索引) * 使用循环,完成数组索引start位置元素与end位置元素值互换。 * 在循环换过程中,每次互换结束后,start位置后移1,end位置前移1 * 在循环换过程中,最先判断start位置是否超越了end位置,若已超越,则跳出循环
10 Array Reverse function implementation
* A: Case code/* Array in reverse order: elements in the array, the position of the exchange reverse is not equal to the reverse traversal is the farthest two index in the array, the position exchange, the implementation of the inverse array using the array The pointer idea is that variables, ideas, can be changed at any time index inversion reverse implementation steps: 1. Define the method to implement the inverse of the array 2. Traversing an array to implement the farthest index of an array using temporary third-party variables */public class arraymethodtest_1{public static void Ma In (string[] args) {int[] arr = {3,5,7,1,0,9,-2}; The reverse method of calling an array is reverse (arr); See the elements of the array, traverse PrintArray (arr); }/* Defines the method that implements the inverse return value of the array: no return value parameter: Array is parameter */public static void Reverse (int[] arr) {//Use loops to implement the array traversal, the first entry of the farthest transposition//for, define 2 variables, and finally, two variable + +--for (int mi n = 0, max = arr.length-1; Min < Max; min++,max--) {//elements in an array, position Exchange//min Index and Max index element interchange//define variable, save min index int temp = Arr[min]; The element on the Max index, assigned to min index arr[min] = Arr[max]; Temporary variables, saved data, assignment to Max index on arr[max] = temp; } } }
11 Selecting the Sorting principle
* A: 选择排序原理 * a: 题目分析(图解见day07_source/选择排序原理.JPG) * 通过观察发现,本题目要实现把数组元素{13,46,22,65,3}进行排序 * 提到数组排序,就要进行元素值大小的比较,通过发现,我们想完成排序要经过若干次的比较才能够完成。 * 中用每圈要比较的第一个元素与该元素后面的数组元素依次比较到数组的最后一个元素,把小的值放在第一个数组元素中,数组循环一圈后,则把最小元素值互换到了第一个元素中。 * 数组再循环一圈后,把第二小的元素值互换到了第二个元素中。按照这种方式,数组循环多圈以后,就完成了数组元素的排序。这种排序方式我们称为选择排序。 * b: 解题步骤 * 使用for循环(外层循环),指定数组要循环的圈数(通过图解可知,数组循环的圈数为数组长度 - 1) * 在每一圈中,通过for循环(内层循环)完成数组要比较的第一个元素与该元素后面的数组元素依次比较到数组的最后一个元素,把小的值放在第一个数组元素中 * 在每一圈中,要参与比较的第一个元素由第几圈循环来决定。如所示 * 进行第一圈元素比较时,要比较的第一个元素为数组第一个元素,即索引为0的元素 * 进行第二圈元素比较时,要比较的第一个元素为数组第二个元素,即索引为1的元素 * 依次类推,得出结论:进行第n圈元素比较时,要比较的第一个元素为数组第n个元素,即数组索引为n-1的元素
12 Select Sort Function implementation
* A: Case code///array sorting: Generally ascending, elements, small to large arrangement two sorts of sort of way select sort: Each element of an array is compared bubble sort: the adjacent elements in the array are compared Rule: Compare size, Position Exchange */public class arraymethodtest_2{public static void Main (string[] args) {int[ ] arr = {3,1,4,2,56,7,0}; Call the Select Sort method//selectsort (arr); PrintArray (arr); }/* Defines the method, implements the selection of the array to sort the return value: No parameters: Array Implementation steps: 1. Nesting loops Implementing sorting The outer loop, which controls how many times the loop is compared, controls how many elements are compared 2 at a time. Determines the size of the element value small value, stored to a small index */public static void Selectsort (int[] arr) {for (int i = 0; i < arr.length-1; i++) {//inner loop, is every time decreasing, modifying the definition of a variable for (int j = i+1; J < Arr.length; J + +) {/ /array elements to be judged if (Arr[i] > Arr[j]) {//array of transposition int temp = arr [i]; Arr[i] = Arr[j]; ARR[J] = temp; }}}}/* Define method, implement function return value: Void method parameter: array */public static void PrintArray (int[] arr) {//output half brackets, do not wrap print System.out.print ("["); The array is traversed for (int i = 0; i < arr.length; i++) {//determines which element is traversed, is not the last element of the array How to determine the loop variable to reach length-1 if (i = = arr.length-1) {//output array elements and] System. Out.print (arr[i]+ "]"); }else{//is not the last element of the array, the output array element and the comma System.out.print (arr[i]+ ","); }} System.out.println (); } }
13 Bubble Sort Feature implementation
* A: bubble sort function Implementation * A: Topic analysis * By observing, this topic to implement the array element {13,46,22,65,3} to sort * Refer to array ordering, we will compare the size of the element value, through the discovery, we It takes several comparisons to complete a sort. * The values of the adjacent elements are compared in turn, the large values are placed in the following elements, and after the loop of the array, the maximum element value is swapped to the last element. Once the array is recycled, the second-largest element value is swapped to the second-to-last element. In this way, the array elements are sorted after the arrays have been looped in circles. This sort of order we call bubble sort. * B: Problem solving steps * using a For Loop (outer loop), specify the number of laps to loop (by illustration, the number of laps in an array loop is 1) * in each lap, the values of adjacent elements are compared by the For loop (inner Loop), and the large value The number of cycles per loop in the back element is determined by the loop of the first circle. As shown in the first loop element comparison, the inner loop number of array length-1 * For the second loop element comparison, the inner loop number of the array length-2 * And so on, concluded that the first When the N-circle element is compared, the inner loop number is an array length-n * C: Case code//The sorting of arrays: Generally ascending, element, small to large arrangement Sort by two sorts of sorts: each element of an array is compared to a bubbling sort: the adjacent elements in the array are compared by the rule: compare size, Position Interchange */public class arraymethodtest_2{public static void Main (string[] args) { INt[] arr = {3,1,4,2,56,7,0}; Call the Select Sort method//selectsort (arr); Call the Bubbling Sort method Bubblesort (arr); PrintArray (arr); }/* Define the method to implement the bubble sort return value for the array: no arguments: Array * /public static void Bubblesort (int[] arr) {for (int i = 0; i < arr.length-1; i++) { The comparison of each inner loop, starting with the 0 index, is decremented each time for (int j = 0; J < Arr.length-i-1; J + +) { The index of the comparison is J and j+1 if (Arr[j] > arr[j+1]) { int temp = Arr[j]; ARR[J] = arr[j+1]; ARR[J+1] = temp; }}}}/* Define method, Implementation features Return value: Void method Parameter: Array */public static void PrintArray (int[] arr) {//output half of the brackets, do not wrap the print System.out.print ("["); The array is traversed for (int i = 0; i < arr.length; i++) {//determines which element is traversed, is not the last element of the array How to tell the loop variable to reach length-1 if (i = = arr.length-1) {//Output number The elements of the group and] System.out.print (arr[i]+ "]"); }else{//is not the last element of the array, the output array element and the comma System.out.print (arr[i]+ ","); }} System.out.println (); } }
Binary lookup principle for 14 arrays
* A: The binary lookup principle of the array (see day07_source/binary Lookup principle for illustration. JPG) * A: Topic analysis * By observing, this topic is implemented to find the location of the specified value stored in an ordered array of elements * (index), return the position (index). * We use the element value in the middle of the array to compare with the specified value to find, * if equal, the index of the value of the middle element is returned * The element value in the middle position is compared with the specified value to find, if not equal, the result of the comparison, * Narrow the query to half the scope of the last array query, update the middle element position according to the new query scope, and then use the intermediate element value to compare with the specified value to find? The comparison result is equal, and the index of the intermediate element value is returned? The comparison results are unequal, and the query continues to shrink by half the range of the last array query, updating the middle element position, continuing the comparison, and so on. * When the query range is reduced to less than 0 elements, the specified value is not queried, and the index value-1 is returned. * B: Problem Solving steps * Define 3 variables to record the index value, the variable min record the minimum index value of the current range, the initial value is 0; * variable max records the maximum index value of the current range, the initial value is an array length-1; The variable mid records the current * The index value of the most intermediate element of the range, the initial value is (MIN+MAX)/2 * Use a loop, judging the current range, the value of the most intermediate element is equal to the value of the specified lookup? If equal, ends the loop and returns the index value of the most intermediate element in the current range, mid? If not equal, based on the results of the comparison, narrow the query to the scope of the previous query general? The value of the intermediate element is larger than the number to query, indicating that the value to be queried is between the minimum and intermediate index positions of the current range, at which point the update query scope is: Range Maximum index value = Last Intermediate index position-1; ? The median value is smaller than the number to query, indicating that the value to be queried is between the current range's maximum index position and the intermediate index position, at which point the update query scope is: Range min index value = Last intermediate index position +1; ? In the new query scope, update the position of the middle element value, using the value of the most intermediate element again to specify whether the found value is equal. Intermediate index value = (range min index value + range Max index value)/2; * Each time the query range is reduced by half, using the IF statement to determine whether the query scope is less than 0 elements, * if less than 0 elements, the specified value is not queried, return index value-1.
15 Binary Lookup code implementation of arrays
* A: Case code/* Array Lookup function in an array, find an element, whether it exists in the array, if present, return the index normal query: Find the index of the element in the array, if there is no This element, the result is a negative number */public class arraymethodtest_3{public static void Main (string[] args) { Int[] arr = {1,3,5,7,9,11,15}; int index = BinarySearch (arr,10); SYSTEM.OUT.PRINTLN (index); }/* Define method, implement, binary find return value: Index parameter: Array, the element to be found implementation steps: 1. The required variable defines three, three pointers 2. The conditions for looping binary can be binary min <= max 3. Allow elements to be found, and intermediate index elements to compare elements > Intermediate index small pointer = middle + 1 Element < Intermediate index Large pointer = middle-1 element = = Intermediate Index found, end, return intermediate index 4. Loop ended, unable to binary element not found, returned-1 */public static int BinarySearch (int[] arr, int key) { Define three pointer variable int min =0; int max = arr.length-1; int mid = 0; Loop binary, Condition Min<=max while (min <= max) {//formula, calculate Intermediate index mid = (Min+max)/2; Let the element be found, and the intermediate index element compare if (Key > Arr[mid]) {min = mid + 1; }else if (Key < Arr[mid]) {max = mid-1; }else{//finds element, returns element index return mid; }} return-1; }/* Defines the method that implements the normal query return value of the array: Index parameter: Array, the element to be searched for implementation steps: 1. Iterate through the array 2. In the traversal process, the elements in the array are compared with elements, and if they are the same, returns a negative number if the index of the element is different, or public static int search (i Nt[] arr, int key) {//traversal array for (int i = 0; i < arr.length; i++) {//array elements, comparison of elements found if (arr[i] = = key) {//Return index return i; }} return-1; } }
07_java Basic Grammar _ 7th Day (Practice) _ Handouts