有字符串数组String[] ar = { "1", "2", "3", "4", "5" }。
1、任意选出三个数,写出所有可能的组合(不可重复,三个数之间也不可重复)。
2、写出一共有多少个组合。

解决方案 »

  1.   

    有时间发这种帖子,还不如去 Google 一下。
      

  2.   

    这个简单
    组合的个数是 
    数组长度 - 一个组合包含多少数
    的差 加上1 然后递减
    最后将 所以数字循环加起来(递归也行)
    例如
    有字符串数组String[] ar = { "1", "2", "3", "4", "5" }。 
    x =(5 - 3 + 1)
    y += x
    x--当然你用循环遍历所以可能的组合
    每次出现符合的组合就打印出来
    并且写一个计数器
    也行  
      

  3.   

    将组合的数存到HashSet中去,重复的会自动去掉,看看我的代码合要求不.
    import java.util.*;public class TestGroup { /**
     * @param args
     */
    public HashSet group(String[] ar){
    HashSet<String> set=new HashSet<String>();
    for(int i=0;i<ar.length;i++){
    for(int j=0;j<ar.length;j++){
    for(int k=0;k<ar.length;k++){
    if(ar[i]==ar[j]&&ar[j]==ar[k])//如果三个数一样就不存进去.
    continue;
    else
    set.add(ar[i]+ar[j]+ar[k]);
    }
    }
    }
    return set;
    }
    public static void main(String[] args) {
    String[] ar=new String[]{"1","2","3","4","5"};
    HashSet<String> set=new HashSet<String>();
    TestGroup test=new TestGroup();
    set=test.group(ar);
    System.out.println(set.size());
    Iterator it=set.iterator();
    while(it.hasNext()){
    System.out.print(it.next()+" ");
    }
    System.out.println("一共有"+set.size()+"个组合");
    }}