- import java.util.Scanner;
- import java.util.TreeSet;
- public class Demo2 {
- public static void main(String[] args) {
- //输入字母
- Scanner sc = new Scanner(System.in);
- String str = sc.nextLine();
- //转化为数组
- char[] ch = str.toCharArray();
-
- TreeSet<String> ts = new TreeSet<String>();
- //先将单个字母存入到集合中
- for(int x=0;x<ch.length;x++)
- {
- ts.add((ch[x]+""));
- }
- // 输出
- System.out.println(ts);
- // 对字母进行无重复组合输出
- for (int x = 0; x < ch.length - 1; x++) {
- //声明过渡集合
- TreeSet<String> tree = new TreeSet<String>();
- tree = forDemo(ts, ch, tree);
- ts = tree;
- }
- sc.close();
- }
- public static TreeSet<String> forDemo(TreeSet<String> ts, char[] ch,
- TreeSet<String> tree)
- {
- for (int x = 0; x < ch.length; x++) {
- String str1 = ch[x] + "";
- for (String str2 : ts) {
- if (!str2.contains(str1)) {
- String temp = new StringBuilder(str2).append(str1)
- .toString();
- tree.add(temp);
- }
- }
- }
- System.out.println(tree.toString());
- return tree;
- }
- }
复制代码
|
|