比如输入字符串"1234", 输出所有的不重复的24个组合:1234,1243,1324,1342,
输入字符串"123", 输出所有的不重复的9个组合:123,132,213,提示:
第一步:求出给定字符串的所有不重复组合数,比如"1234"的组合是24个,"123"是9个,"1233"是9个,
第二步:用到Hashtable和collections.shuffle()如果不用上面的提示用其它的方法也行
输入字符串"123", 输出所有的不重复的9个组合:123,132,213,提示:
第一步:求出给定字符串的所有不重复组合数,比如"1234"的组合是24个,"123"是9个,"1233"是9个,
第二步:用到Hashtable和collections.shuffle()如果不用上面的提示用其它的方法也行
String[] list;
StringBuffer sb = new StringBuffer();
int len,start,x;
test(String in){
len = Integer.parseInt(in);
list = new String[len];
for(int i=0;i<len;i++)
list[i] = String.valueOf(i+1);
printList();
System.out.print(sb);
}
void replaceStr(int c1,int c2){
String temp = list[c1];
list[c1] = list[c2];
list[c2] = temp;
}
void printList(){
if(start==len-1){
if(x++>10000000){
x=0;
System.out.print(sb);
sb = new StringBuffer();
}
for(int i=0;i<len;i++,x++)
sb.append(list[i]);
sb.append(" ");
}else{
for(int i=start;i<len;i++){
replaceStr(start++,i);
printList();
replaceStr(i,--start);
}
}
}
public static void main(String args[]){
new test(args[0]);
}
}
String[] list;
StringBuffer sb = new StringBuffer();
int start;
test(String in){
list = in.split(",");
printList();
System.out.print(sb);
}
void replaceStr(int c1,int c2){
String temp = list[c1];
list[c1] = list[c2];
list[c2] = temp;
}
void printList(){
if(start==list.length-1)
for(int i=0;i<list.length;i++)
sb.append(list[i]+(i==list.length-1?"\n":","));
else
for(int i=start;i<list.length;i++){
replaceStr(start++,i);
printList();
replaceStr(i,--start);
}
}
public static void main(String args[]){
new test(args[0]);
}
}
这个是用,来分割字符串的
比如1,2,3,4,dd,ff
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;public class Ruader {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String sin = in.nextLine().replaceAll("[^0-9]", "");
while (sin.equals(""))
sin = in.nextLine().replaceAll("[^0-9]", "");
ArrayList<Character> ch = new ArrayList<Character>();
int setSize = 1;
for (int i = 0; i < sin.length(); i++) {
ch.add(sin.charAt(i));
setSize *= i + 1;
}
HashSet<String> set = new HashSet<String>();
while (set.size() < setSize) {
StringBuilder sb = new StringBuilder();
for (Character t : ch)
sb.append(t);
set.add(sb.toString());
Collections.shuffle(ch);
}
for (String t : set)
System.out.println(t);
}
}