[Daily Blue Bridge] 30, 15 years provincial Java group real question "total number of cards"

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: Total number of cards

Xiao Ming was hijacked to Casino X and forced to play cards with the other three.

A deck of playing cards (without the big and small kings, a total of 52) is evenly divided among 4 people, each with 13 cards,

At this time, a question suddenly appeared in Xiao Ming's mind:

If you do not consider suits, only points, and not the order of the cards you get, how many kinds of initial card combinations can you get in your hand?

Please fill in the whole number, do not fill in any redundant content or explanatory text.

Problem-solving ideas:

For the solution of this problem, recursive thinking is mainly used to think about the problem in another way:

Suppose we now have 13 playing cards (the playing cards are exactly the same), and now we need to place these 13 cards in 13 positions from 1 to 13 (ie A, 2, 3...J, Q, K of the playing cards) , Regulations: Only 0~4 cards can be placed in each sequence, so how many ways are there in total?

According to the above situation, we can directly use the for loop and recursion to place the A, 2, 3...J, Q, and K of the playing cards in order. When the last sequence is placed (the sequence may not have poker Cards), and when 13 cards have been placed, the probability is increased by 1.

Answer source code:

public class Year2015_Bt7 {

	static int ans=0;
	public static void main(String[] args) {
		f(0,0);
		System.out.println(ans);
	}

	
	/**
	 * @param k 当前确定是的哪一位扑克牌
	 * @param cnt 已经确定的扑克牌个数
	 * */
	private static void f(int k, int cnt) {
		//如果待确定的扑克牌序列或已经确定的扑克牌个数大于13,则跳出
		if (k>13||cnt>13) return;
		//如果待确定的扑克牌序列和已经确定的扑克牌个数都等于13,则可能性加一
		if (k==13&&cnt==13) {
			ans++;
			return;
		}
		//因为每一种牌最少0张,最多4张,所以循环范围是0~4,
		//且刚开始确定的是序列1
		for (int i = 0; i < 5; i++) {
			//确定下一个序列,且已经确定的扑克数要相应的加i
			f(k+1, cnt+i);
		}
		
	}
		
		
	}
	

 

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/114753832