[Daily Blue Bridge] 36, 2016 provincial competition Java group real question "make up formula"

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: Imputation formula

Problem-solving ideas:

The idea of ​​solving this question is: First, the 9 numbers from 1 to 9 should be arranged. After the arrangement, the number installation problem in the array requires division. At this time, we should pay attention to: the three data obtained are all fractions, and the denominator Not the same. At this time, we need to divide the data equally to get three numbers with the same denominator, and then add these three numbers to determine whether the obtained numerator is 10 times the denominator. If it is, it means that the arrangement is next The situation is true. Friends who don’t understand the combination, arrangement, and full arrangement of array elements can read this article of mine, [Recursion + Backtracking] Realize the combination, arrangement and full arrangement of array elements

The problem of permutation and combination is also one of the high-frequency test sites of the Blue Bridge Cup in recent years, and it must be mastered!

Answer source code:

public class Year2016_Bt3 {

	static int ans = 0;
	public static void main(String[] args) {
		int [] arr = {1,2,3,4,5,6,7,8,9};
		f(arr,0);
		System.out.println(ans);
	}

	//将数组元素进行全排列
	private static void f(int[] arr, int k) {
		//如果数组已经排列完毕
		if (k==arr.length) {
			check(arr);
		}
		
		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;
		}
	}

	//判断排列好的元素是否符合题目要求
	private static void check(int[] arr) {
		int a = arr[3]*100+arr[4]*10+arr[5];//百位数的分子
		int b = arr[6]*100+arr[7]*10+arr[8];//百位数的分母
		int mol = arr[0]*arr[2]*b + arr[1]*b + a*arr[2];//同分后的分子
		int den = arr[2]*b;//同分后的分母
		//如果同分之后,分子是分母的10倍,则说明符合题目要求
		if (den*10==mol) {
			System.out.println(arr[0] + " + " + arr[1] + "/" + arr[2] + " + " + a + "/" + b);
			ans++;
		}
		               
	}

}

 

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