加入一个字符串“abcd”,怎么输出:
a
b
c
d
ab
ac
ad
bc
bd
cd
abc
...
abcd
这个问题主要是想问问能不能实现一个方法,输入的参数为这个字符串,而且这个字符串是可以为任何长度的,即 假如方法为output(String aa),aa可以为“abcd”也可以为“hello".我主要是不能实现这个重用的方法。谢谢

解决方案 »

  1.   

    递归:
    public static void printString(String str, int n){
    for(int i = 0; i <= str.length() - n; i++){
    System.out.println(str.substring(i, i + n));
    }
    if(n < str.length()){
    printString(str, ++n);
    }
    }

    public static void main(String[] args){
    printString("hello", 1);
             }
      

  2.   

    static void combinate(String s){
            if(s.length()>30) return; //too large
            int loop=1<<s.length();
            for(int i=1;i<loop;i++){
                StringBuffer sb=new StringBuffer();
                for(int j=0;j<s.length();j++){
                    if((i&(1<<j))>0) sb.append(s.charAt(j));
                }
                System.out.println(sb.toString());
            }
        }