基础测试有这么一道题,我的想是嵌套循环,但是很片面,如果输入3个以上字符的字符串,我这程序就没用。那位大神教教思路。 
 
 
/** 
* 第七题:        编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符,例如: 
 
                原始字符串是"abc",打印得到下列所有组合情况: 
                "a" "b" "c"  
                "ab" "bc" "ca" "ba" "cb" "ac" 
                "abc" "acb" "bac" "bca" "cab" "cba" 
 
*/ 
 
import java.io.*; 
class Test7 
{ 
        public static void main(String[] args) throws Exception 
        { 
                while(true) 
                { 
                         //键盘录入。 
                        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); 
                        String line = bufr.readLine(); 
                        if("886".equals(line)) 
                                break; 
                        //转换成字符数组。 
                        char[] ch = line.toCharArray(); 
                       //单个字符。 
                        for(int x=0;x<ch.length;x++) 
                                System.out.print("“"+Character.toString(ch[x])+"”"); 
                        System.out.println(); 
                        //两个字符,每个位置都对字符数组遍历,除掉相同的。 
                        for(int x=0;x<ch.length;x++) 
                        { 
                                for(int y=0;y<ch.length;y++) 
                                { 
                                        if(x!=y) 
                                                System.out.print("“"+Character.toString(ch[x])+Character.toString(ch[y])+"”"); 
                                        continue; 
                                } 
                        } 
                        System.out.println(); 
                         //三个字符组合。 
                        for(int x=0;x<ch.length;x++) 
                        { 
                                for(int y=0;y<ch.length;y++) 
                                { 
                                        for(int z=0;z<ch.length;z++) 
                                        if(x!=y&&x!=z&&y!=z) 
                                                System.out.print("“"+Character.toString(ch[x])+Character.toString(ch[y])+Character.toString(ch[z])+"”"); 
                                        continue; 
                                } 
                        } 
                        System.out.println(); 
                } 
        } 
} 
 
 |