Simulate the next_permutation function!

Source: Internet
Author: User
Now there is a simple question, that is, output all the numbers of 1 -- n! For example, if n = 5 is input, the numbers 1, 2, 3, 4, and 5 must be obtained! C ++ has a next_permutation function, which is included in the algorithm header file, allowing you to conveniently find all the arrays. But do you know how it works? Now, I have simulated that function. The simulated function is as follows:
/************************* Output non-recursive algorithms of all permutation numbers. After calculation testing, feasible! Simulate the next_permutation function! To put it simply, we can use 1, 2, 3, and 4 to obtain the intermediate values of the original permutation: 1 (3 * (3) + 2) * (2) + 1) * (1) = 231,2, 3, 2, 0 (3 * (3) + 2) * (2) + 0) * (1) = 221,3, 1 (3 * (3) + 1) * (2) + 1) * (1) =, 3, 1, 0 (3 * (3) + 1) * (2) + 0) * (1) =, 1 (3 * (3) + 0) * (2) + 1) * (1) = 19 ........., 0, 0 (0 * (3) + 0) * (2) + 0) * (1) = 0 the intermediate conversion above refers: the number of digits after each digit greater than the current digit. For example, in, 1 is followed by (3, 4, 2) They are greater than 1, so the first is 3, 3 is followed by (4, 2 ), but only 4 is greater than 3, so the second digit is 1 4 followed by (2), and there is no bigger than 4, so the third digit is 0, and the last digit is certainly not bigger, therefore, 0 is omitted. After this conversion, a representation (intermediate conversion) is obtained, which corresponds to the original arrangement and can be converted to each other. Observe this intermediate expression carefully and find that the first and second bits can only be (,), the second bits can only be (, 2), and the third bits can only be ). Generally, numbers are represented in decimal notation and binary in the computer. But now, I use a special hexadecimal notation to represent numbers: the first digit is in hexadecimal notation, the second digit is in binary format... Then we get the decimal value of this intermediate representation. For example, level | 1, 1, 0 ----> (1 * (3) + 1) * (2) + 0) * (1) = 8, 3, 1, 0 ----> (3 * (3) + 1) * (2) + 0) * (1) = 20, you can get a one-to-one correspondence between a decimal number and an arrangement. Now there is a one-to-one correspondence between the number of permutation and the ordered decimal number (by changing the correspondence, you can make the decimal number in ascending order ). By now, we can easily get any arrangement. According to the above correspondence, there are a total of possible values (four values can be obtained for the highest bit, and three ''' for the second bit '''') 4*3*2*1 = 24; The above is actually just a form, in order to facilitate understanding the model set up! * ************************/# Include <iostream> usingnamespacestd; constmax = 50; INTA [Max]; intpermutation (INTN) // permutation function {Inti, J, TMP, flag = 1; for (I = N; i> = 2 & flag/* this newly learned, can easily exit multiple cycles */; I --) if (a [I]> A [I-1]) // compare each adjacent two. If the first one is smaller than the latter (I is a smaller position, II is a larger position ), at this time, there must be a larger order than the current {for (j = N; j> = 2 & flag; j --) // you should find the first number after this small number starting from the end to be greater than it {if (a [J]> A [I-1]) // change it to the current small position {TMP = A [J]; A [J] = A [I-1]; A [I-1] = TMP; flag = 0/* Find this number It is equivalent to finding a sequence, and then you can return */;} If (! Flag) // a large number II to the end of the reverse order, that is, the exchange, generates the next arrangement, the principle is similar to, find the next large number as the starting bit, then, sort the following numbers in ascending order {TMP = A [I]; A [I] = A [n]; A [n] = TMP; /* In fact, this method adds the number of sorted reverse orders to 1 */} according to the above method. // when the number of reverse orders is the largest, it cannot be arranged.} If (FLAG) return0; elsereturn1;} intmain (intargc, char * argv []) {INTN, I; while (1) {CIN> N; for (I = 1; I <50; I ++) A [I] = I; do {for (I = 1; I <= N; I ++) cout <A [I] <""; cout <Endl;} while (permutation (n); // always generate an order until the number of reverse orders <according to the above method> (from large to small) is 0 ;} return0 ;}
 
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.