生成“选择”组合的组合。操作 [英] Generating the combinations of a "choose" operation

查看:67
本文介绍了生成“选择”组合的组合。操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要执行 6选择2操作的变体。我写了下面的代码。

I want to perform a variation of a "6 choose 2" operation. I have written the following code.

public void choosePatterns(){
    String[] data = {"1", "2", "3", "4", "5", "6"};
    String[] originalPattern = new String[15];
    int index = 0;
    for(int i = 0; i < (6-1); i++)
    {
        for(int j = i+1; j < 6; j++)
        {
            System.out.println(i + "," +j);
        }
    }
}

到目前为止,我的代码会全部生成 6选择2的可能组合。但是,我想改变这个并打印所有剩余的数字。到目前为止,如果 6选择2的组合之一是 3和 4,那么我想打印 1, 2, 5, 6。

My code so far generates all the possible combinations of "6 choose 2." However, I would like to vary this and print all the remaining numbers. So far instance, if one of the combinations of "6 choose 2" is "3" and "4," then I would like to print "1," "2," "5," "6."

我不确定如何最有效地做到这一点。漫长的方法是删除数据数组中的那些索引,移动数据以确保没有间隙,然后打印该数组。

I am not sure how to most efficiently do this. The long-winded way would be to delete those indices in the "data" array, shift the data so there aren't gaps, then print the array. But is there a faster, more efficient way to do this?

推荐答案

这是一种方法。

public static void choosePatterns(int total, int toChoose) {
  List<Integer> indices = IntStream.range(1, toChoose + 1).mapToObj(i -> Integer.valueOf(0)).collect(Collectors.toList());
  resetIndex(indices, 0, 1, total);
  while (true) {
    System.out.println("chosen: " + indices);
    System.out.print("not chosen: ");
    for (int i = 1; i <= total; i++) {
      if (! indices.contains(Integer.valueOf(i))) {
        System.out.print(i + " ");
      }
    }
    System.out.println("\n");

    if (! incrementIndices(indices, indices.size() - 1, total)) {
      break;
    }
  }
}

public static boolean resetIndex(List<Integer> indices, int posn, int value, int total) {
  if (value <= total) {
    indices.set(posn, value);
    return posn == indices.size() - 1 ? true : resetIndex(indices, posn + 1, value + 1, total);
  } else {
    return false;
  }
}

public static boolean incrementIndices(List<Integer> indices, int posn, int total) {
  if (indices.get(posn) < total) {
    indices.set(posn, indices.get(posn) + 1);
  } else {
    int resetPosn = posn;
    do {
      if (resetPosn-- == 0) return false;
    } while (! resetIndex(indices, resetPosn, indices.get(resetPosn) + 1, total));
  }
  return true;
}

public static void main(String[] args) throws IOException {
  choosePatterns(6, 2);
}

打印以下内容:

chosen: [1, 2]
not chosen: 3 4 5 6 

chosen: [1, 3]
not chosen: 2 4 5 6 

chosen: [1, 4]
not chosen: 2 3 5 6 

chosen: [1, 5]
not chosen: 2 3 4 6 

chosen: [1, 6]
not chosen: 2 3 4 5 

chosen: [2, 3]
not chosen: 1 4 5 6 

chosen: [2, 4]
not chosen: 1 3 5 6 

chosen: [2, 5]
not chosen: 1 3 4 6 

chosen: [2, 6]
not chosen: 1 3 4 5 

chosen: [3, 4]
not chosen: 1 2 5 6 

chosen: [3, 5]
not chosen: 1 2 4 6 

chosen: [3, 6]
not chosen: 1 2 4 5 

chosen: [4, 5]
not chosen: 1 2 3 6 

chosen: [4, 6]
not chosen: 1 2 3 5 

chosen: [5, 6]
not chosen: 1 2 3 4 

这篇关于生成“选择”组合的组合。操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆