All integers of any object in C ++

Source: Internet
Author: User

Bid is coming to an end.CodeSome of them need to be fully arranged for integers. For example, the full sorting of 0 1 3 is 013 031 103 130 301. I thought it was a little complicated to write a full arrangement. I didn't want to do it. I directly switched the case, because there were three elements at most.

In the end, we hope to expand the test scope. The number of elements in the full arrangement increases to 4, and switch case again, it will be slow, and it will be too technical. Therefore, we simply write a fully arrangedAlgorithm.

STD: vector < Int > Quanpailie (STD: vector < Int > & V) {STD: Vector < Int > Result;
If(V. Empty ())ReturnResult; If (V. Size () =1 ) {Result. push_back (V [ 0 ]); Result. push_back ( - 1 ); Return Result ;} For ( Int I = 0 ; I <v. Size (); ++ I ){ Int Head = V [I]; STD: Vector <Int > T (V); T. Erase (T. Begin () + I); t = Quanpailie (t); Result. push_back (head ); For ( Int J = 0 ; J <t. Size ()- 1 ; ++ J ){ If (J> = 1 & T [J- 1 ] =- 1 ) {Result. push_back (head);} result. push_back (T [J]);} result. push_back ( - 1 );} Return Result ;} Int Main ( Int Argc, Char * Argv []) { Int A [] = { 1 , 2 , 3 , 4 }; STD: Vector < Int > T; For ( Int I = 0 ; I < 4 ; ++ I) T. push_back (A [I]); t = Quanpailie (t ); Return 1 ;}

 

The algorithm is recursive. It should be said that the efficiency is not too high. It is best to change vector to list.

In addition, the algorithm applies to any object. You only need to change int to another object. Of course, you also need to change the delimiter of other objects. In this example, the separator is-1.

That is to say, the result is in a full arrangement of 1 2 3-1 1 3 2-1 3 3 3 1 2-1 and so on.

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.