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:
//function function: Get every bit, if the other number is smaller than the current number of possibilities total//a current number (small to large)//n Current number total function GetAll (a,n) {var sum=1;//total for (Var i=n;i> ; 1;i--) Sum=sum*i; Calculates the total probability of n ordinal positions placed n different numbers return sum* (A-1)/n;
Calculate the total number of probabilities that are smaller than the number of the first for a (a)//m the sequence of numbers to be computed//a the number of digits in the current bit and the number of its rear digits, its size ordinal numbers//an 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, and a small function find (m) {m= (m+ ""). Split ("");
Split the current number into an array to make it easy to compute the 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 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 ("array A:", a);
for (i=1,sum=1;i<m.length;i++) {sum+=getall (+a[i-1],m.length-i+1);//Loop call GETALL calculates the total probability of a combination of each digit with the number following it is 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 Console.log (Find (21)); Output 2 Console.log (Find (1)); Output 1