package com.itheima;
import java.util.Scanner;
public class Test7 {
/**
* 7、编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符,例如: 原始字符串是"abc",打印得到下列所有组合情况: "a" "b"
* "c" "ab" "bc" "ca" "ba" "cb" "ac" "abc" "acb" "bac" "bca" "cab" "cba"
*
* 思路:1,将字符串拆分为单个字符 2,使用递归算法进行组合
*
*/
public static void main(String[] args) {
// System.out.println("请输入所要转换的字母或单词!");
// //定义并初始化键盘读入语句
// Scanner sc = new Scanner(System.in);
// //声明字符串接收从键盘读取的字符串
// String str = sc.nextLine();
print("sah");
}
public static void print(String str) {
int x = str.length();
String[] ch1 = new String[x];
String[] ch2 = new String[x];
for (int i = 0; i < str.length(); i++) {
ch1[i] = str.substring(i, i + 1);
ch2[i] = str.substring(i, i + 1);
}
myout(ch1, ch2);
}
public static void myout(String[] ch1, String[] ch2) {
StringBuffer sbf = new StringBuffer();
String[] ch3 = null;
int k = 1;
k++;
for (int i = 0; i < ch1.length; i++) {
for (int j = 0; j < ch2.length; j++) {
// System.out.println(ch2.length);
if (!ch1[i].contains(ch2[j])) {
String s = ch1[i] + ch2[j];
sbf.append(s);
sbf.append("*");
}
}
}
// 切割sbf
String s = sbf.toString();
System.out.println(s);
String regex = "[*]";
ch3 = s.split(regex);
for (String string : ch3) {
System.out.println(string);
}
if (k <= ch1.length) {
myout(ch1, ch3);
}
}
}
|