高手帮忙!
“用1、2、2、3、4、5这五个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求: "4 "不能在第三位, "3 "与 "5 "不能相连。”
哪位大侠指教一哈!
------解决方案--------------------Permutation p = new Permutation(6,6);
p.startWithZero();
Vector v = p.getAll();
Set s = new HashSet();
int[] val = {1,2,2,3,4,5};
int k = 0;
for(int i = 0; i < v.size();++i){
int[] cur = (int[])v.elementAt(i);
if(val[cur[2]]==3)
continue;
StringBuffer b = new StringBuffer( " "+val[cur[0]]);
boolean isBad = false;
for(int j = 1; !isBad && j < cur.length; ++j){
isBad = val[cur[j]]==3 && val[cur[j]]==5 || val[cur[j]]==5 && val[cur[j]]==2;
b.append( " "+val[cur[j]]);
}
if(isBad || s.contains(b.toString()))
continue;
s.add(b.toString());
for(int j = 0; j < cur.length; ++j)
System.out.print(val[cur[j]]+ " ");
System.out.println( "--> "+(k++)+ " "+i);
}
// total 300 records