[Daily Blue Bridge] 44. The real question of the 2017 provincial Java group "Card Triangle"

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: Solitaire Triangle

A, 2, 3, 4, 5, 6, 7, 8, 9 are arranged in a total of 9 cards-an equilateral triangle (A is calculated by 1). The sum of each side is required to be equal.

The figure below shows--sorting method (if there is alignment problem, please refer to p1.png).

There may be many such arrangements.

If you consider the same after rotation and mirroring, how many different arrangements are there?

Please calculate and submit this figure.

Note: What needs to be submitted is-an integer, do not submit any extra content.

Problem-solving ideas:

For this question, in fact, according to the style of the blue bridge cup, the first thing we think of should be the full arrangement of the nine numbers from 1 to 9. After that, we judge the arranged sequence and calculate the three sides. The sum of the values ​​of each side determines whether the three values ​​are equal. As for which numbers in the arranged array are on which side, you can see the figure below. The numbers in the figure represent the subscripts of the array.

int a1 = arr[0] + arr[1] + arr[3] + arr[5];

int a2 = arr[0] + arr[2] + arr[4] + arr[8];

int a3 = arr[5] + arr[6] + arr[7] + arr[8];

Just judge whether a1, a2, a3 are equal

After calculating all the permutations that meet the requirements, pay attention: since each face can be rotated 3 times, and there are mirrors 2 times, it will repeat 3*2=6 times, so the answer needs to be divided by 6.

Answer source code:

public class Year2017_Bt2 {

	static int [] initArr = {1,2,3,4,5,6,7,8,9};
	static int ans = 0;
	public static void main(String[] args) {
		f(initArr,0);
		//由于每一个面可以旋转3次,且有镜面2次,所以会重复3*2=6次,所以答案需要除以6
		System.out.println(ans/6);
	}
	
	//对数组元素进行排列
	private static void f(int[] arr, int k) {
		
		if(k==arr.length) {
			//计算出每一个面的数值总和
			int a1 = arr[0] + arr[1] + arr[3] + arr[5];
			int a2 = arr[0] + arr[2] + arr[4] + arr[8];
			int a3 = arr[5] + arr[6] + arr[7] + arr[8];
//			如果三个面的数值都相等,则成立
			if (a1==a2&&a2==a3) {
				ans++;
			}
		}
		
		for (int i = k; i < arr.length; i++) {
			int t = arr[k];
			arr[k] = arr[i];
			arr[i] = t;
			
			f(arr, k+1);
			
			t = arr[k];
			arr[k] = arr[i];
			arr[i] = t;
		}
		
	}

}

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