[Daily Blue Bridge] 22. Four-year provincial Java group real question "Strange Fraction"

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: Strange Fraction

When he was in elementary school, Xiao Ming often invented new algorithms by himself. Once, the teacher asked:

1/4 times 8/5

Xiao Ming actually spliced ​​the numerators together and the denominators together. The answer is: 18/45 (see Figure 1.png)

The teacher just wanted to criticize, and then I thought about it, this answer happened to be right, what a hell!

For the case where the numerator and denominator are both a single digit from 1 to 9, what other formulas can be calculated like this?

Please write down the number of all different calculations (including the examples in the question)

Obviously, after swapping the numerator and denominator, for example: 4/1 multiplied by 5/8 is enough to meet the requirements, this is counted as a different formula

But for the same numerator and denominator, 2/2 times 3/3 are too many types to count!

Note: The answer is an integer, (considering symmetry, it must be an even number), please submit it through the browser, and do not write extra content.

Problem-solving ideas:

This question mainly uses four for loops in the calculation, assuming that the two formulas are a/b and c/d.

Then we have to enumerate all the possibilities of a, b, c, and d, and then according to the requirements in the question, we should reduce the two fractions obtained from the result to see if the two numbers are equal after the reduction .

The main method used is to find the greatest common divisor of two numbers and the reduction of fractions.

Answer source code:

public class Year2014_Bt6 {

	public static void main(String[] args) {
		int ans = 0;
		for (int a = 1; a < 10; a++) {
			for (int b = 1; b < 10; b++) {
				for (int c = 1; c < 10; c++) {
					for (int d = 1; d < 10 ; d++) {
						if (a==b&&c==d) {
							continue;
						}
						int gcd1 = gcd(a*c, b*d);
						int gcd2 = gcd(a*10+c, b*10+d);
						if ((a*c)/gcd1 == (a*10+c)/gcd2&&(b*d)/gcd1 == (b*10+d)/gcd2) {
							ans++;
						}
					}
				}
				
			}
			
		}
		System.out.println(ans);
	}
	
	/**
	 * 求两个数的最大公约数
	 * */
	static public int gcd(int a,int b) {
		if (a%b==0) {
			return b;
		}
		return gcd(b, a%b);
	}
		
}

 

 

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