Efficient arrangement and combination algorithms (implemented in Java)

Source: Internet
Author: User

  1. Package beanutil;
  2. Import java. util. arraylist;
  3. Import java. util. List;
  4. Import com. Work. Core. Exception. ourexception;
  5. /**
  6. * A combination of the most likely three to appear
  7. *
  8. * @ Author wangmingjie
  9. * @ Date 2009-1-1 1-1 01:22:19 pm
  10. */
  11. Public class copy_2_of_statisanythree {
  12. // Combined algorithm
  13. // The idea of this program is to open an array, whose subscript represents the number of 1 to m, and the value of the array element is 1 to indicate its subscript.
  14. // Indicates that the number is selected. If it is 0, it is not selected.
  15. // First initialize, set the first n elements of the array to 1, indicating that the first combination is the first n.
  16. // Scan the "10" combination of array element values from left to right, find the first "10" combination, and change it
  17. // "01" combination, and move all "1" on the left to the leftmost end of the array.
  18. // When the first "1" is moved to the M-N position of the array, that is, when all N "1" are moved to the rightmost end, you must
  19. // The last combination.
  20. // Example:
  21. // 1 1 1 0 0 // 1, 2, 3
  22. // 1 1 0 1 0 // 1, 2, 4
  23. // 1 0 1 1 0 // 1, 3, 4
  24. // 0 1 1 1 0 // 2, 3, 4
  25. // 1 1 0 0 1 // 1, 2, 5
  26. // 1 0 1 0 1 // 1, 3, 5
  27. // 0 1 1 0 1 // 2, 3, 5
  28. // 1 0 0 1 1 // 1, 4, 5
  29. // 0 1 0 1 1 // 2, 4, 5
  30. // 0 0 1 1 1 // 3, 4, 5
  31. Public static void main (string [] ARGs ){
  32. Copy_2_of_statisanythree S = new copy_2_of_statisanythree ();
  33. S. printanythree ();
  34. }
  35. /**
  36. *
  37. */
  38. Public void printanythree (){
  39. Int [] num = new int [] {1, 2, 3, 4, 5, 6 };
  40. Print (combine (Num, 3 ));
  41. }
  42. /**
  43. * Select M numbers from N numbers
  44. * @ Param
  45. * @ Param m
  46. * @ Return
  47. */
  48. Public list combine (INT [] A, int m ){
  49. Int n = A. length;
  50. If (M> N ){
  51. Throw new ourexception ("error! Array A contains only "+ N +" elements. "+ M +" greater than "+ 2 + "!!! ");
  52. }
  53. List result = new arraylist ();
  54. Int [] BS = new int [N];
  55. For (INT I = 0; I <n; I ++ ){
  56. BS [I] = 0;
  57. }
  58. // Initialization
  59. For (INT I = 0; I <m; I ++ ){
  60. BS [I] = 1;
  61. }
  62. Boolean flag = true;
  63. Boolean tempflag = false;
  64. Int Pos = 0;
  65. Int sum = 0;
  66. // First locate the first 10 combination, then convert it to 01, and move all the 1 on the left to the leftmost of the array
  67. Do {
  68. Sum = 0;
  69. Pos = 0;
  70. Tempflag = true;
  71. Result. Add (print (BS, a, m ));
  72. For (INT I = 0; I <n-1; I ++ ){
  73. If (BS [I] = 1 & BS [I + 1] = 0 ){
  74. BS [I] = 0;
  75. BS [I + 1] = 1;
  76. Pos = I;
  77. Break;
  78. }
  79. }
  80. // Move all 1 on the left to the leftmost of the array
  81. For (INT I = 0; I <Pos; I ++ ){
  82. If (BS [I] = 1 ){
  83. Sum ++;
  84. }
  85. }
  86. For (INT I = 0; I <Pos; I ++ ){
  87. If (I <sum ){
  88. BS [I] = 1;
  89. } Else {
  90. BS [I] = 0;
  91. }
  92. }
  93. // Check if all 1 is moved to the rightmost
  94. For (INT I = N-m; I <n; I ++ ){
  95. If (BS [I] = 0 ){
  96. Tempflag = false;
  97. Break;
  98. }
  99. }
  100. If (tempflag = false ){
  101. Flag = true;
  102. } Else {
  103. Flag = false;
  104. }
  105. } While (FLAG );
  106. Result. Add (print (BS, a, m ));
  107. Return result;
  108. }
  109. Private int [] print (INT [] BS, int [] A, int m ){
  110. Int [] result = new int [m];
  111. Int Pos = 0;
  112. For (INT I = 0; I <BS. length; I ++ ){
  113. If (BS [I] = 1 ){
  114. Result [POS] = A [I];
  115. Pos ++;
  116. }
  117. }
  118. Return result;
  119. }
  120. Private void print (list l ){
  121. For (INT I = 0; I <L. Size (); I ++ ){
  122. Int [] A = (INT []) L. Get (I );
  123. For (Int J = 0; j <A. length; j ++ ){
  124. System. Out. Print (A [J] + "/t ");
  125. }
  126. System. Out. println ();
  127. }
  128. }
  129. }

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.