0
点赞
收藏
分享

微信扫一扫

【蓝桥杯Java_C组·从零开始卷】第六节(二)、蓝桥杯常用数学公式

三维控件研究 2022-01-05 阅读 6

目录

1、欧几里得定理

2、最大公约数

3、最小公倍数

4、海伦公式(求三角形面积)

5、排序公式 


1、欧几里得定理

package Action;

public class demo {
	/*
	 * 求最大公约数 最小公倍数 思路:根据欧几里得定理 gcd(a,b)=gcd(b,a%b);
	 */
	static int gcd(int a, int b) {
		// 出口:b=0;5和0的最大公约数是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、最大公约数

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、最小公倍数

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、海伦公式(求三角形面积)

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、排序公式 

 暂时能想到这些,后面的再补充啊。

举报

相关推荐

0 条评论