当前位置:   article > 正文

n选m 组合 算法的next函数(非递归方法)_非递归组合算法 n中取m组合

非递归组合算法 n中取m组合
  1. /*
  2. *@auther 皇阿玛
  3. *2019.9.16
  4. */
  5. package courseOne;
  6. import java.util.Arrays;
  7. class Combination implements ppppp{
  8. int total;//总人数
  9. private int[] people;//当前所选的人
  10. public Combination() {}
  11. public Combination(int total,int selectPeople) {
  12. this.people=new int[selectPeople];
  13. this.total=total;
  14. Integer a = 2;
  15. }
  16. public int[] nextTwo() {
  17. //判断数组是否为空
  18. int max=total-1;
  19. int flag=0;
  20. for(int i=0;i<people.length;i++) {
  21. flag+=people[i];
  22. }
  23. if(flag==0) {
  24. //初始化数组
  25. for(int i=0;i<people.length;i++) {
  26. people[i]=i;
  27. }
  28. }else {
  29. for(int i=people.length-1;i>=0;i--) {
  30. if(people[i]<max) {
  31. people[i]=people[i]+1;
  32. //当该位置的数据进行到判断前一位置时
  33. if(i<people.length-1) {
  34. /**
  35. * version:3.0
  36. * answer:right
  37. * @author MrGao
  38. */
  39. for(int j=i+1;j<people.length;j++) {
  40. //从前往后遍历数组,在之前数据的基础上加一
  41. int temp = people[j-1]+1;
  42. //如果当前修改数据超出预定数据,跳出
  43. if(temp==total-1) {
  44. break;
  45. }
  46. //将修改后的数据修改到源数据
  47. people[j]=temp;
  48. }
  49. }
  50. break;
  51. }else {
  52. max = max-1;
  53. }
  54. }
  55. }
  56. return people;
  57. }
  58. public int arrangement(int[][] array) {
  59. int len = people.length;
  60. int weight = 0;//求取某一个组合的权值之和
  61. for(int i = 0;i < len;i++) //写一个双重for循环,因为是二维数组
  62. {
  63. for(int j = 0;j < len;j++)
  64. {
  65. weight += array[ people[i] ][ people[j] ];
  66. }
  67. }
  68. // Combination comb = new Combination(5,3);
  69. return weight;
  70. }
  71. }
  72. public class CombinationExample {
  73. public static void main(String[] args) {
  74. int[][] test=new int[][] {
  75. {10,5,1,4,6},
  76. {0,11,9,8,4},
  77. {5,3,9,9,7},
  78. {4,4,5,12,6},
  79. {3,1,7,6,10}
  80. };
  81. //输出数组
  82. Combination comb = new Combination(5,3);
  83. int sum=10;
  84. int TotalWeight = 0; //计算最终的最大的权值之和
  85. int[] zuhe={};
  86. while(sum>0) {
  87. int[] a =comb.nextTwo(); //求取下一个组合,以数组的形式保存
  88. int tem = comb.arrangement(test);
  89. //CombinationExample combEx = new CombinationExample();
  90. //int tem = combEx.arrangement(test, a);
  91. sum--;
  92. //当前组合已经存在a[i]中了,含有的元素个数为 selectPeople 个
  93. // TotalWeight =(TotalWeight>tem)? TotalWeight:tem ;
  94. if(TotalWeight < tem)
  95. {
  96. TotalWeight = tem;
  97. zuhe = a;
  98. }
  99. }
  100. System.out.println(TotalWeight+" "+Arrays.toString(zuhe));
  101. }
  102. // private int weight = 0;//求取的组合之权值和
  103. public int arrangement(int[][] array,int[] people) {
  104. int len = people.length;
  105. int weight = 0;//求取某一个组合的权值之和
  106. for(int i = 0;i < len;i++) //写一个双重for循环,因为是二维数组
  107. {
  108. for(int j = 0;j < len;j++)
  109. {
  110. weight += array[ people[i] ][ people[j] ];
  111. }
  112. }
  113. return weight;
  114. }
  115. }

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/590479
推荐阅读
相关标签
  

闽ICP备14008679号