Java的组合算法 [英] Java combination algorithm

查看:172
本文介绍了Java的组合算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于整数的集合,什么是Java的算法,这将使组合,如下:

Given a collection of integers, what's a Java algorithm that will give combinations as follows..

给出的示例集合:[1,3,5],我们会想输出:

Given the example collection: [1,3,5], we'd want the output:

[1-1]
[3-3]
[5-5]

[1-3]
[1-5]
[3-5]

请注意该顺序并不重要的,所以我们希望之一[1-3],[3-1]但不能同时

Note that ordering is not important, so we want one of [1-3], [3-1] but not both.

此应与n个数,不只是这三个数字作为在本实施例的集合

This should work with a collection of n numbers, not just the the three numbers as in this example.

推荐答案

下面的功能应该做到这一点。

Below function should do this

  private void printPermutations(int[] numbers) {
    for(int i=0;i<numbers.length; i++) {
      for (int j=i; j<numbers.length; j++) {
        System.out.println("[" + numbers[i] + "-"+ numbers[j] +"]");
      }
    }
  }

实例调用该函数

Example call to this function

int[] numbers={1,2,3};
printPermutations(numbers);

这篇关于Java的组合算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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