扑克排序
A A 2 2 3 3 4 4, 一共4对扑克牌。请你把它们排成一行。
要求:两个A中间有1张牌,两个2之间有2张牌,两个3之间有3张牌,两个4之间有4张牌。
请填写出所有符合要求的排列中,字典序最小的那个。
例如:22AA3344 比 A2A23344 字典序小。当然,它们都不是满足要求的答案。
请通过浏览器提交答案。“A”一定不要用小写字母a,也不要用“1”代替。字符间一定不要留空格。
思路分析: 全排列来得到所以排列顺序 再根据题意要求列出条件进行筛选 最后将数据放入 set 数组中
这里不用list是因为list有去重机制 最后使用字典排序进行摘除
字典排序是一种对于随机变量形成序列的排序方法,其方法是按照字母排列顺序,或数字顺序由小到大形成的的序列。
数字的优先级高于字母
/**
* @version 1.0
* @auther 孙沐华
* 字典序
*/
public class test07 {
static HashSet set=new HashSet();
public static void main(String[] args) {
String str="AA223344";
char[] a = str.toCharArray();
f(a,0);
for (Object o : set) {
System.out.println(o);
}
}
public static void f(char[] a,int m){
if (m==a.length){
String str=new String(a);
if (check(str)){
set.add(str);
}
}
for (int i = m; i <a.length ; i++) {
char n=a[m];
a[m]=a[i];
a[i]=n;
f(a,m+1);
n=a[m];
a[m]=a[i];
a[i]=n;
}
}
public static boolean check(String str){
if (str.lastIndexOf("A")-str.indexOf("A")==2&&str.lastIndexOf("2")-
str.indexOf("2")==3&&str.lastIndexOf("3")-str.indexOf("3")==4&&str.lastIndexOf("4")-str
.indexOf("4")==5){
return true;
}
else return false;
}
}
答案 :2342A3A4
浙公网安备 33010602011771号