Java programming 47-array Example 3

Source: Internet
Author: User
Tags array example integer division

Java programming those things 47-array use example 3 Zhengzhou game college Chen yuefeng from: http://blog.csdn.net/mailbomb 6.3.7 digital statisticsRequirement: count the number that appears the most in an integer. If the number of numbers is the same, the maximum number is used. For example, 1 outputs 1,121 output, 1 outputs output, and 3 Outputs output. This is a comprehensive question, which can be divided into three problems in actual analysis: 1. Split each number in an integer, 2. count the number of each 0-9 in the split number. 3. obtain the maximum number of digits. Implementation logic: 1. Split numbers: integers and 10 to obtain the bitwise value of the integer. Then, divide the integer by 10 to remove the bitwise (integer division ), use this structure to implement loops and store the split numbers (I .e. the remainder) in the array. 2. statistical number: declare an integer array with a length of 10. Use the first element in the array to save the number 0, and the second element to save the number 1, and so on. Use loops to calculate the number of digits. 3. Obtain the number corresponding to the maximum value: Obtain the subscript of the maximum value in the number array, which is the required number. The implementation code is as follows: int M = 1232312; int [] n = new int [10]; // store the split number int num = 0; // store the number of split numbers while (M! = 0) {// n [num] = m % 10; // obtain the single digit num ++; // increase the number of split numbers by 1 m/= 10; // remove the split number} int [] Count = new int [10]; // store the number of times 0-9 numbers appear // count the number of times the numbers appear for (INT I = 0; I <num; I ++) {count [n [I] ++;} // obtain the maximum subscript int Index = 0; For (INT I = 0; I <count. length; I ++) {If (count [Index] <= count [I]) {Index = I ;}// output system. out. println (INDEX); in this Code, the decimal number to be split is first split into a single digit and stored in the N array, and then the number to be split is removed by division 10, continue to execute the loop until M is 0, and the variable num stores the split number Number. Use the array count to remember the number of occurrences of each number 0-9, Count [0] to store the number of occurrences of 0, Count [1] to store the number of occurrences of 1, and so on, so when the value of N [I] is, you only need to increase the value of Count [n [I] by 1. Finally, use the loop to obtain the subscript of the maximum number, and apply the <= comparison to ensure that when the number is the same as the subsequent number, you can obtain the subscript of the maximum number through the loop, according to the array count structure, the subscript of the array and the value of the number. 6.3.8 array EncodingRequirement: There is an array of A, length is N, internal data is all numbers between 0 to N-1, for example, when n is equal to 5, the array is: a =, 4 }. For array A, there is a corresponding encoding array B. The length of B is equal to the length of a. The values of elements in array B are as follows: the values of A and B [0] are 0b and B [I], which are the number of elements smaller than the values of a [I] in array. C. In the example, the value of B in the encoded array {0, 3, 4} corresponding to array a is {0, 1, 1, 4 }. Now we know array A, and the encoding code calculates the corresponding encoding array B. This is a basic array transformation question. If you are familiar with the question requirements, you can solve the corresponding array B according to the question requirements. Implementation idea: initialize a B array with the same length as array A, initialize the value of the first element to 0, and calculate the number of digits smaller than the [I] element in a loop, assign a value to B [I. The implementation code is as follows: int [] A = {0, 3, 2, 1, 4}; int [] B = new int [. length]; B [0] = 0; // initialize the first element. Optional. For (INT I = 1; I <. length; I ++) {int COUNT = 0; // count variable // count the number of elements less than a [I] For (Int J = I-1; j> = 0; j --) {if (a [J] <A [I]) {count ++ ;}} B [I] = count; // value assignment} in this Code, the number of elements earlier than a [I] is counted according to the value of array B, then, assign the obtained result to B [I] to fulfill the requirement of the question. 6.3.9 array sortingRequirement: sort the elements in the array in ascending order. Array sorting is the basis for many array operations. In actual use, there are also many sorting methods. Here we use Bubble Sorting as an example to describe the array sorting algorithm. Implementation idea: one element is sorted each time, and the total number of sorting times is reduced by one length of the array. First, compare the first and second elements. If the first element is larger than the second element, exchange the values of the two elements and then compare the second and third elements, if the second one is greater than the third one, it is switched, and so on. After the first exchange is complete, the last element in the array must be the largest element in the array. At the second time, only one element is subtracted from the Front Length of the array. The comparison steps are the same as those for the first time, and so on. Each time an element is compared, the sorted array is obtained. The implementation code is as follows: int [] M = {2, 10, 3, 4, 2}; For (INT I = 0; I <m. length-1; I ++) {// Number of sorting times // compare two to achieve sorting for (Int J = 0; j <m. length-1-I; j ++) {If (M [J]> M [J + 1]) {// exchange int temp = m [J]; M [J] = m [J + 1]; m [J + 1] = temp ;}}// output the sorted element for (INT I = 0; I <m. length; I ++) {system. out. println (M [I]);} by comparing and exchanging elements in the array, the elements in the array are sorted. The loop variable "I" indicates the number of sorting times, and the total number of sorting times is reduced by 1 by the length of the array. The inner cycle variable is J and the unordered elements are compared by two. The cyclic condition can ensure that I is increased by 1, and the inner comparison elements are reduced by 1, this function is not to compare the elements in the descending order.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.