[Blue Bridge Cup java_group C ยท volume from scratch] section 6. Common mathematical formulas of Blue Bridge Cup

catalogue

1. Euclidean theorem

2. Maximum common divisor

3. Least common multiple

4. Helen formula (find triangle area)

5. Sorting formula

1. Euclidean theorem

package Action;

public class demo {
	/*
	 * The idea of finding the least common multiple of the greatest common divisor: according to Euclidean theorem gcd(a,b)=gcd(b,a%b);
	 */
	static int gcd(int a, int b) {
		// Outlet: B = 0; The greatest common divisor of 5 and 0 is 5
		if (b == 0)
			return a;
		return gcd(b, a % b);
	}

	static int lcm(int a, int b) {
		return a * b / gcd(a, b);
	}

	public static void main(String[] args) {
		System.out.println(gcd(45, 35));
		System.out.println(lcm(45, 35));
		System.out.println(gcd(42, 60));
		System.out.println(lcm(42, 60));
	}
}

2. Maximum common divisor

Problem description
What is the greatest common divisor of 70044 and 113148?
Answer submission
This is a question to fill in the blanks. You just need to calculate the results and submit them. The result of this question is an integer. When submitting the answer, only fill in this integer. If you fill in the redundant content, you will not be able to score.

package Action;

public class demo {
	public static void main(String[] args) {
		int max = 0;
		for (int i = 1; i <= 70044; i++) {
			if (70044 % i == 0 && 113148 % i == 0) {
				max = i;
			}
		}
		System.out.println(max);
	}
}

3. Least common multiple

Problem description

Given a positive integer N, ask what is the maximum common multiple of any three numbers selected from 1~N.

Input format

Enter a positive integer N.

Output format

Output an integer representing the least common multiple you find.

sample input

9

sample output

504

Find three numbers in a group to maximize their least common multiple. First of all, start with the greatest common multiple of two numbers. When two numbers are coprime, the least common multiple is their product. Of course, this is also the largest least common multiple. Therefore, expanding to three numbers is to find three numbers that are coprime. One conclusion we have to know first is that two adjacent natural numbers are coprime.

In a group of numbers from 1 to N, the largest three numbers are N,N-1 and N-2. If these three numbers are mutually prime, they are of course the largest least common multiple.

According to the above conclusion, N and N-1, N-1 and N-2 are mutually prime.

If N is odd, then N-2 is also odd. Then these two numbers will not be divided by 2, nor will they be divided by 3, 5, 7 and 9 at the same time. So these two numbers are also mutually prime. Therefore, when N is an odd number, N * (N - 1) * (N - 2) is the largest least common multiple.

If n is an even number, obviously both N and N-2 will be divided by 2, so (N-2) is changed to (N-3). If n cannot be divided by 3, the largest least common multiple is N * (N - 1) * (N -3). If n can be divided by 3, change n to (N-1), and the maximum least common multiple is (N - 1) * (N - 2) * (N - 3). Here (N - 1) is an odd number, just like the case where n is an odd number.

package Action;

import java.util.Scanner;

public class demo {
	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		if (n % 2 == 0) {
			if (n % 3 == 0) {
				long m = (n - 1) * (n - 2) * (n - 3);
				System.out.println(m);
			} else {
				long m = n * (n - 1) * (n - 3);
				System.out.println(m);
			}
		} else {
			long m = n * (n - 1) * (n - 2);
			System.out.println(m);
		}
	}
}

4. Helen formula (find triangle area)

Algorithm to improve triangle area
Time limit: 1.0s memory limit: 256.0MB
Problem description
Find the area of a triangle from the length of its three sides.
Tip: the following formula can be used to calculate the area from the three sides a, B and C of the triangle:

Input format
Three integers separated by spaces.
Output format
A real number with two decimal places.
sample input
3 4 5
sample output
6.00
Data scale and agreement
The three input edges must form a triangle without judgment. a. B, C less than 1000

package Action;

import java.util.Scanner;

public class demo {
	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		double l = (a + b + c) * 1.0 / 2;
		double s = Math.sqrt(l * (l - a) * (l - b) * (l - c));
		System.out.println(String.format("%.2f", s));
	}
}

5. Sorting formula

I can think of this for the time being. I'll add it later.

Keywords: Java C Math

Added by outpost on Thu, 06 Jan 2022 02:59:21 +0200