[Daily Blue Bridge] 2. The real question of the Java group in the provincial competitions for the first and third years "group prime number"

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: Group Prime

A prime number is a number that can no longer be divided equally, such as: 2, 3, 5, 7, 11, etc.

9=3*3 means it can be divided into 3 equal parts, so it is not a prime number

Our country was founded in 1949. If you only give you the 4 digital cards 1, 9, 4, and 9, you can place them in any order, (but the cards can’t be placed backwards, we are not doing a brain teasing )

So how many 4-digit prime numbers can you form?

For example, 1949 and 4919 meet the requirements

Please submit the number of 4-digit prime numbers that can be formed, do not list these prime numbers! !

Note: Do not submit the answering process or other supporting text

Focus of investigation:

Recursive and backtracking algorithms for the complete arrangement of elements, the judgment of prime numbers, and the removal of duplicate elements

In this question, what we need to focus on is the full arrangement of elements under the recursive and backtracking algorithms. According to this idea, the four numbers 1, 9, 4, and 9 are all arranged to obtain the arrangement result, and then the arrangement result Make a judgment to determine whether it is a prime number, and then record the number of prime numbers that meet the requirements.

Answer source code:

package 一三年省赛真题;

import java.util.HashSet;
import java.util.Set;

public class Year2013_t2 {

	static Set<Integer> set = new HashSet<Integer>();	//定义hash集合,存放符合要求的数值
	/**
	 * 数组元素的全排列
	 * @param arr 数据数组
	 * @param n	从第几个元素开始排列
	 * */
	public static void arrange(int[] arr,int n) {
		//如果排列的数值的序号等于数组的长度,说明最后一个元素已经确定
		if (n==arr.length) {
			int num = arr[0]*1000 + arr[1]*100 + arr[2]*10 + arr[3];
			//如果是素数
			if (check(num)==num) {
				set.add(num);	//将数值添加到集合中,并去重
			}
		}
		for (int i = n; i < arr.length; i++) {
			//数值交换
			int t = arr[n];
			arr[n] = arr[i];
			arr[i] = t;
			
			arrange(arr, n+1);    //确定下一个数值
			
			t = arr[n];
			arr[n] = arr[i];
			arr[i] = t;
			
		}
	} 
	
	/**
	 * 判断一个数是不是素数
	 * @return 是素数就返回该数,不是就返回0
	*/
	private static int check(int num) {
		for (int i = 2; i <= Math.sqrt(num); i++) {
			if (num % i == 0) {
				return 0;
			}
		}
		return num;
	}


	public static void main(String[] args) {
		int[] array = {1,9,4,9};
		arrange(array, 0);
		System.out.println(set.size());	//输出存放在集合中的符合要求的数据长度
	}

}

 

Sample output:

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

Interested friends can follow the column!

Little Grey Ape will accompany you to make progress together!

 

Guess you like

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