Combined problem and code implementation, combined problem code implementation

Source: Internet
Author: User

Combined problem and code implementation, combined problem code implementation

Recursive Implementation of combination Problems

Taking the number of m (0 <m <= n) in n as an example, the problem can be divided:

1. First, select the maximum number of n numbers, and then select the number of m-1 in n-1-1 numbers until 1 is selected from the number of n-(m-1.
2. Select a smaller number from n to continue with Step 1 until the maximum number of available numbers is m.

Obviously, the above method is a recursive process. That is to say, the recursive method can be used to obtain all combinations cleanly.

The implementation of recursive methods is as follows:
(1) calculate all the combinations of any m elements from array a [1 .. n.
(2) a [1. n] indicates the candidate set. n is a small candidate set, and n> = m> 0.
(3) B [1. M] is used to store the elements in the current combination (the element subscript is stored here ),
(4) constant M indicates the number of elements in a combination that meets the conditions. M = m. These two parameters are only used to output results.

Code implementation:

  1. # Include <iostream>
  2. Using namespace std;
  3. Void combine (int a [], int n, int m, int B [], const int M)
  4. {
  5. For (int I = n; I> = m; I --) // pay attention to the loop range.
  6. {
  7. B [M-1] = I-1;
  8. If (m> 1)
  9. Combine (a, I-1 m-1, B, M );
  10. Else // m = 1, output a combination
  11. {
  12. For (int j = M-1; j> = 0; j --)
  13. Cout <a [B [j] <"";
  14. Cout <endl;
  15. }
  16. }
  17. }
  18. Int main ()
  19. {
  20. Const int N = 4;
  21. Int a [N];
  22. For (int I = 0; I <N; I ++)
  23. A [I] = I + 1;
  24. For (int M = 1; M <= 4; M ++)
  25. {
  26. // Recursive Method
  27. Int B [M];
  28. Combine (a, N, M, B, M );
  29. }
  30. Return 0;
  31. }
Running result






Question about JAVA code arrangement and combination

Package test;
Public class tt {

/**
* @ Param args
*/
Public static void main (String [] args ){
String [] arr = {"a", "B", "c", "d", "e", "f", "g", "h ", "I", "j", "k", "l", "m", "n", "o", "p", "q", "r ", "s", "t", "u", "v", "w", "x", "y", "z"
, "0", "1", "2", "3", "4", "5", "6", "7", "8 ", "9 "};
Int h = 0;
For (int I = 0; I <arr. length; I ++)
{
For (int j = 0; j <arr. length; j ++)
{
For (int k = 0; k <arr. length; k ++)
{
System. out. print (arr [I] + arr [j] + arr [k] + "");
H ++;
}
System. out. println ();
}
}
}

}
I forgot about unicode encoding, and I wrote it directly. Founder implemented it. Haha

For the code of the compound arrangement and combination !!

No one can solve the problem.

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.