[Daily Blue Bridge] 23. Four-year provincial Java group real question "Poker Sequence"

Hello, I am the little gray ape, a programmer who can write bugs!

Welcome everyone to pay attention to my column " Daily Blue Bridge ". The main function of this column is to share with you the real questions of the Blue Bridge Cup provincial competitions and finals in recent years, analyze the algorithm ideas, data structures and other content that exist in it, and help you learn To more knowledge and technology!

Title: Poker Sequence

AA 2 2 3 3 4 4, a total of four pairs of playing cards, please line them up,

Requirements: There is a card between the two Aces, two cards between the two 2s, three cards between the two 3s, and four cards between the two 4s.

Please fill in the one with the smallest lexicographical order among all the permutations that meet the requirements.

For example: 22AA3344 is lexicographically smaller than A2A23344. Of course, none of them satisfy the requirements.

Please submit the answer through the browser. Do not use lowercase a for "A", and do not use "1" instead, and do not leave spaces between characters.

Problem-solving ideas:

In the solution of this question, the idea of ​​permutation and combination is mainly investigated. Use the permutation and combination to enumerate all the possible results of 8 playing cards, and then determine whether the combination meets the requirements given in the question. Here, if you want to output the smallest Arrangement order, then it is necessary to compare the size of the string, the method used here is the compareTo method.

Assuming that a and b are two strings, the method to compare the size of a and b is:

a.compareTo(b)

a==b returns 0

a>b The return value is greater than 0

a<b The return value is less than 0

Answer source code:

package 一四年省赛真题;

import java.util.ArrayList;
import java.util.List;

/**标题:扑克序列
A A 2 2 3 3 4 4,一共四对扑克牌,请你把它们排成一行,
要求:两个A中间有一张牌,两个2之间有两张牌,两个3之间有三张牌,两个4之间有四张牌。
请填写出所有符合要求的排列中,字典序最小的那个。
例如:22AA3344比A2A23344字典序小,当然,它们都不是满足要求的答案。
请通过浏览器提交答案,“A”一定不要用小写字母a,也不要用“1”代替,字符间一定不要留空格。
*/
public class Year2014_Bt7 {
	
	static String minString = "AA223344";	//定义最小的字符串
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("A");
		list.add("A");
		list.add("2");
		list.add("2");
		list.add("3");
		list.add("3");
		list.add("4");
		list.add("4");
		f(list,0);
		System.out.println(minString);
	}

	/**
	 * 列表元素的全排列
	 * */
	private static void f(List<String> list,int n) {
		if (n==list.size()) {
			if (check(list)) {
				String ansList = "";
				for (String string : list) {
					ansList+=string;
				}
				//判断当前的字符串是否小于新组成的字符串
				/**
				 * a.compareTo(b)方法
				 * a==b 返回0
				 * a>b 返回值大于0
				 * a<b 返回值小于0
				 * */
				if (minString.compareTo(ansList)>0) {
					minString=ansList;
				}
			}
		}
		for (int i = n; i < list.size(); i++) {
			String t = list.get(n);
			list.set(n, list.get(i));
			list.set(i, t);
			
			//递归确定下一个元素
			f(list, n+1);
			
			//回溯,返回原序列
			t = list.get(n);
			list.set(n, list.get(i));
			list.set(i, t);
		}
		
		
	}

	/**
	 * 检查组合是否符合要求
	 * */
	private static boolean check(List<String> list) {
		if (list.lastIndexOf("A")-list.indexOf("A")==2
				&&list.lastIndexOf("2")-list.indexOf("2")==3
				&&list.lastIndexOf("3")-list.indexOf("3")==4
				&&list.lastIndexOf("4")-list.indexOf("4")==5) {
			return true;
		}
		return false;
	}

}

 

 

Sample output:

There are deficiencies or improvements, and I hope that my friends will leave a message and learn together!

Interested friends can follow the column!

Little Gray Ape will accompany you to make progress together!

Guess you like

Origin blog.csdn.net/weixin_44985880/article/details/113696817