Javascript algorithm question: Calculate the sequence number of any 1-9 N-digits that are not repeated in the combination.

Source: Internet
Author: User

Javascript algorithm question: Calculate the sequence number of any 1-9 N-digits that are not repeated in the combination.

The specific question is as follows:

Select N numbers from 1-9 to Form N-digit numbers that are not repeated. Numbers are numbered from small to large. When you enter any number M, you can find the corresponding number.

. For example, N = 3, M = 213. output: [123 (1), 132 (2), 213 (3), 231 (4), 312 (5), 321 (6)] ---> X = 2

First, we can see that the question is to generate an array in full arrangement from the smallest to the largest, and then traverse the array to get the corresponding serial number (array subscript plus 1 ), or, you may think of generating push-in arrays from small to large, and then judging whether the number is the number given by the current question. If yes, the required sequence number is the length of the current array, better than above, you do not need to waste time computing and generating the following items. The generation itself is not complex. If it is extended to a hexadecimal or even a 36-hexadecimal system and a large number is given, it is not good. There is also a waste of space to save useless data. Maybe we can try other methods that do not need to be generated.

Let's idealize the question first. If we give a number N, then M is composed of 1-N digits (for example, N = 4, then M is composed of 1234 digits, instead of other 1349 or other combinations ). This is because we need to simplify the conditions so that we can analyze the common methods to solve the problem, and it is not difficult to convert from a random situation to an ideal situation. First, analyze the examples given by the following questions: [123 (1), 132 (2), 213 (3), 231 (4), 312 (5), 321 (6)] 213 is in the third place, and the first digit is 2. That is to say, the first digit is in front of him (123,132). Let's look at the combination of the second digit and the next digit 13, the first letter 1 is already the smallest, and there cannot be any number in front of him, and the third number 3 does not need to be read, because if the first digit is determined, the last digit has only one possibility. The result is that the first digit of 213 is 2 (first digit) + 0 (two digits) + 0 (last digit) = 2 digits, that is to say, the current number is at 3rd bits. The answer is true, and the analysis of other numbers is the same. From this we can obtain that we need a function (that is, the setAll () of the following code) to calculate the total number of possibilities of a certain number smaller than the current number, and then accumulate + 1 as the expected result, see the code implementation:

// Function: obtain each bit. If it is another number, the total number of possibilities is smaller than the current number. // the sequence number of the current number of a (from small to large) // n current count function getAll (a, n) {var sum = 1; // total count for (var I = n; I> 1; I --) sum = sum * I; // calculates the likelihood of placing n different numbers in n ordered positions. return sum * (A-1)/n; // calculate the total number of possibilities that are smaller than the current number when the first digit is a} // The number sequence to be calculated by m // a stores the number of the Current BIT and the number of the last bit the size of the number. // For example, the value of array a of 213 is [2, 1, 1]; a [0] is 2 because the first part of 213 2 is 213 small in the third digit; a [1] is 1 because the first 1 of 13 ranks the first small function find (m) {m = (m + "") in 13 ""). split (""); // You can split the current number into arrays to calculate var a = new Array (m. length + 1 ). join (1 ). split (""); // quickly generate an array whose length is m and the value is 1. for the function description of array a, see the comments in the above function header for (var I = 0; I <m. length-1; I ++) {for (var j = I + 1; j <m. length; j ++) {if (+ m [I]> + m [j]) a [I] ++ ;}// generate a array console. log ("a array:", a); for (I = 1, sum = 1; I <m. length; I ++) {sum + = getAll (+ a [I-1], m. length-I + 1 ); // call getAll cyclically to calculate the total number of combinations after each bit is smaller than the current combination.} return m + "the number in the full Arrangement" + sum + "bit ";} console. log (find (213); // output 3console. log (find (123); // output 1console. log (find (231); // output 4 console. log (find (312); // output 5console. log (find (4321); // output 24console. log (find (21); // outputs 2console. log (find (1); // output 1

Related Article

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.