蓝桥杯-扑克序列

蓝桥杯->扑克序列

1441185437

摘要:本题是2014年第五届蓝桥杯全国软件大赛预赛A组第6题,结果填空题。

题目描述

标题:扑克序列
A A 2 2 3 3 4 4, 一共4对扑克牌。请你把它们排成一行。
要求:两个A中间有1张牌,两个2之间有2张牌,两个3之间有3张牌,两个4之间有4张牌。
请填写出所有符合要求的排列中,字典序最小的那个。
例如:22AA3344 比 A2A23344 字典序小。当然,它们都不是满足要求的答案。
请通过浏览器提交答案。“A”一定不要用小写字母a,也不要用“1”代替。字符间一定不要留空格。
解题思路

本题可以看成对字符串的处理,思路很清晰, 遍历字符串的全部排序,找到符合的字符串即可。
满足排序的有[2342A3A4, 4A3A2432],取第一个即可。
代码实现

package lanqiaobei;

import java.util.HashSet;

/**

  • @date 2015-9-2
  • @author Ape
  • /

public class PuKeList {

private static HashSet<String> set = new HashSet<String>();

public static void putStr(StringBuilder s1, StringBuilder s2) {
    if (s1.length() == 0 && find(s2.toString()))
        set.add(s2.toString());
    for (int i = 0; i < s1.length(); i++)
        put(new StringBuilder(s1).deleteCharAt(i),
                new StringBuilder(s2).append(s1.charAt(i)));
}

public static void main(String[] args) {
    String str = "AA223344";
    putStr(new StringBuilder(str), new StringBuilder());
    System.out.println(set);
}

public static boolean find(String str) {
    int a = str.indexOf("A");
    int a1 = str.lastIndexOf("A");
    int b = str.indexOf("2");
    int b1 = str.lastIndexOf("2");
    int c = str.indexOf("3");
    int c1 = str.lastIndexOf("3");
    int d = str.indexOf("4");
    int d1 = str.lastIndexOf("4");
    if (a1 - a == 2 && b1 - b == 3 && c1 - c == 4 && d1 - d == 5)
        return true;
    return false;
}

}