如I1,I2,I3,I4,I5两两组合
成:怎么用java实现:,组成后是{I1,I2},{I1,I3}.......的

解决方案 »

  1.   

    用list 或者是 数组来顺序存放
    之后两层循环我觉得是大致应该是这样的。
      

  2.   

    for(int i = 1 ; i <= 5 ; i ++){
    for(int j = i + 1 ; j <= 5 ; j ++){
    System.out.print("I" + i + "I" +j + "   ");
    }
    }
      

  3.   

      和打印小学的加法口诀一个道理!list list2=new Arraylist();
    for(int i=0;i<list.length;i++){
      for(int j=i+1;i<list.length;i++){       list2.add(list[j]);    }
      }
      

  4.   

    package test;import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;public class Test { public String[] array = { "I1", "I2", "I3", "I4", "I5" }; public Set printArrayCom() {
    Set set = new HashSet();
    for (int i = 0; i < array.length; i++) {
    for (int j = i + 1; j < array.length; j++) {
    String[] comStr = new String[2];
    comStr[0] = array[i];
    comStr[1] = array[j];
    if (!set.contains(comStr)) {
    set.add(comStr);
    }
    }
    }
    return set;
    } public static void main(String[] args) {
    Set re = new Test().printArrayCom();
    Iterator it = re.iterator();
    while (it.hasNext()) {
    String arr[] = (String[]) it.next();
    System.out.println(arr[0] + " ," + arr[1]);
    }
    }
    }
      

  5.   

    双重for循环就可以做到了 啊
      

  6.   

    我的意思不仅仅只是把他们打印出来啊,最重要的是还要把两两组合的结果再次存到一个数组里面
    如:{"I1I2","I1I3".............}这样要怎么实现呢
      

  7.   


    public class Array { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String [] s = {"I1","I2","I3","I4","I5"};
    String [] newStr = new String[(s.length*(s.length-1))/2];
    int count = 0;
    for(int i = 0 ;i<s.length;i++){
    for(int j = i+1;j<s.length;j++){
    newStr[count++] = s[i]+s[j];
    }
    }
    for(int k = 0 ; k<newStr.length;k++){
    System.out.println(newStr[k]);
    }
    }}
      

  8.   

    标准组合数算法:
    import java.util.*;public class CConbination {
    private List<Integer> CCList = new ArrayList<Integer>();
    private List<Integer> firstCC = new ArrayList<Integer>();
    private List<Integer> lastCC = new ArrayList<Integer>(); public CConbination(int m, int n) {
    try {
    if (m > n) {
    throw new Exception("组合数溢出!");
    }
    for (int i = 0; i < n; i++) {
    CCList.add(i);
    if (i < m)
    firstCC.add(i);
    if (i > n - m - 1)
    lastCC.add(i);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }// 找到第一个不存有自己最终内容的位置
    private int opIdx(List<Integer> curList) {
    int idx = -1;
    for (int i = curList.size() - 1; i >= 0; i--) {
    if (curList.get(i) != lastCC.get(i)) {
    return i;
    }
    }
    return idx;
    }// 组合工具的next()函数把当前数组内容按照字典序求得下一个数组内容
    // next()函数设计:从右向左,找到第一个不存有自己最终内容的位置。把这个位置上的数值加1,后面的数值递增 public List<Integer> next(List<Integer> curList) {
    int idx = this.opIdx(curList);
    if (idx == -1) {
    return null;
    } else {
    curList.set(idx, curList.get(idx) + 1);
    for (int i = idx + 1; i < curList.size(); i++)
    curList.set(i, curList.get(i - 1) + 1);
    return curList;
    }
    } public static void main(String args[]) {
    CConbination cc = new CConbination(2, 4);
    List<Integer> curList = cc.firstCC;
    int count = 0;
    while (curList != null) {
    count++;
    curList = cc.next(curList);
    }
    System.out.println("count=" + count);
    }
    }
      

  9.   


    本人不递归用的不太明白,但我还是用了老长时间用递归写了一个(不知道对不对),如下:
    public class Test {
    private static String recursive(String [] str,int index){
    String temp = "";

    for (int i = index; i < str.length; i++) {
    int temp1 =i;
    if(str.length>i+1){
    temp += str[index]+ str[i+1];
    temp += ",";
    }

    }
    temp = temp.substring(0,temp.length()-1);
    if(index+1<str.length-1){
    temp +=","+recursive(str,index+1);
    }
    return temp;
    }
    public static void main(String[] args) {
    String arr[] = {"I1","I2","I3","I4","I5"};
    String result[] = recursive(arr,0).split(",");
    for (int i = 0; i < result.length; i++) {
    System.out.println(result[i]);
    }

    }
    }