JavaScript algorithm title for any one 1-9-bit not repeated n-digit number in the combination of the size of the sequence number _javascript tips

Source: Internet
Author: User
The specific topic is this:

Select n numbers from the 1--9, make up the number of n digits that are not duplicated, numbering from small to large, and when you enter any number m, you can find the number corresponding

The number. such as n=3,m=213. Output: [123 (1), 132 (2), 213 (3), 231 (4), 312 (5), 321 (6)]--->x=2

The first thing that comes to mind is to generate an array of all permutations from small to large, then go through the array to get the corresponding ordinal number (array subscript plus 1), or think of a small to large generation push into the array, and then determine whether the number is the current problem, if it is the number of the current array is the length, The better thing than the previous one is that you don't have to waste time calculating the items behind the build. The complexity of the build itself is not high, if extended to 16 or even 36 into the system and give a large number of words is not good, there is a need to waste a part of the space to save data. Maybe we can try other methods that don't have to be built.

Let's idealize the subject, and if we give a number n, then M is made up of 1-n n digits (like n=4, which is a combination of 1234 numbers, not other 1349, etc.). The reason why we do this is because we have to simplify the conditions to analyze the common way to solve problems, and to change from a random situation to the ideal situation is not difficult, this article is not verbose. First analyze the example given under the title, [123 (1), 132 (2), 213 (3), 231 (4), 312 (5), 321 (6)] 213 in third place, the first number is 2, that is, the first number is 1 in front of him (123,132), and then look at the combination of the second number and the following number 1 3, the first letter 1 is already the smallest, he can't have any number in front, and the third number 3 does not need to look, because if the previous digits are determined, the last one is only possible, the result is 213 of the front 2 (first) +0 (two bit) +0 (tail bit) = 2 number, This means that the current number is in the 3rd position, contrast the answer is true, the other number of analysis is the same. From this we can conclude that we want a function (that is, the SetAll () of the following code) to work out the total number of probabilities that are smaller than the current number, and then add up to +1 is the desired result, see Code implementation:

Copy Code code as follows:

function function: Get each digit, if the other number is smaller than the current total probability
A current number (from small to large)
N Total Current number
function GetAll (a,n) {
var sum=1; Total
for (var i=n;i>1;i--) sum=sum*i; The total number of possibilities to put n different numbers in N ordered positions
Return sum* (A-1)/n; Calculates the total probability of a number smaller than the current number of the first.

m a sequence of numbers to compute
A number that consists of the number of digits in the current bit and the number of its rear digits, its size ordinal
For example, a array of 213 is [2,1,1]; A[0] is 2 because 213 first-digit 2 is the 2nd-largest in 2,133 digits, and a[1] is 1 because 13 is the first 1 in 13.
function Find (m) {
M= (m+ ""). Split (""); Split the current number into an array to make it easy to compute each one.
var a=new Array (m.length+1). Join (1). Split (""); Quickly generate an array of 1 of the length of length m, a description of the function of a array look at the comment on the function header above
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 array A
Console.log ("A array:", a);
for (i=1,sum=1;i<m.length;i++) {
Sum+=getall (+a[i-1],m.length-i+1); Circular Call GetAll calculates the total probability of a combination of each digit and subsequent number smaller than the current combination
Return m+ "ranked in the full ranked first" +sum+ ";
Console.log (Find (213)); Output 3
Console.log (Find (123)); Output 1
Console.log (Find (231)); Output 4
Console.log (Find (312)); Output 5
Console.log (Find (4321)); Output 24
Console.log (Find (21)); Output 2
Console.log (Find (1)); Output 1
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.