From 1ba2367740fadd52ba040e1015c23c0c4005c2c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A9=E8=BE=95=E9=BE=99=E5=84=BF?= Date: Wed, 5 Apr 2023 12:06:21 +0800 Subject: [PATCH] =?UTF-8?q?2427:=E5=85=AC=E5=9B=A0=E5=AD=90=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/NumberOfCommonFactors.java | 62 +++++++++++++++++++ .../cn/doc/content/NumberOfCommonFactors.md | 28 +++++++++ 2 files changed, 90 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/NumberOfCommonFactors.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/NumberOfCommonFactors.md diff --git a/src/main/java/leetcode/editor/cn/NumberOfCommonFactors.java b/src/main/java/leetcode/editor/cn/NumberOfCommonFactors.java new file mode 100644 index 0000000..5c0a8cc --- /dev/null +++ b/src/main/java/leetcode/editor/cn/NumberOfCommonFactors.java @@ -0,0 +1,62 @@ +////给你两个正整数 a 和 b ,返回 a 和 b 的 公 因子的数目。 +// +// 如果 x 可以同时整除 a 和 b ,则认为 x 是 a 和 b 的一个 公因子 。 +// +// +// +// 示例 1: +// +// 输入:a = 12, b = 6 +//输出:4 +//解释:12 和 6 的公因子是 1、2、3、6 。 +// +// +// 示例 2: +// +// 输入:a = 25, b = 30 +//输出:2 +//解释:25 和 30 的公因子是 1、5 。 +// +// +// +// 提示: +// +// +// 1 <= a, b <= 1000 +// +// +// Related Topics 数学 枚举 数论 👍 28 👎 0 + + +package leetcode.editor.cn; + +// 2427:公因子的数目 +public class NumberOfCommonFactors { + public static void main(String[] args) { + Solution solution = new NumberOfCommonFactors().new Solution(); + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int commonFactors(int a, int b) { + int cnt = 0; + while (a != b) { + int sub = Math.abs(a - b); + a = Math.min(a, b); + b = sub; + } + for (int i = 1; i * i <= a; i++) { + if (a % i == 0) { + if (a / i == i) { + cnt++; + } else { + cnt += 2; + } + } + } + return cnt; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/doc/content/NumberOfCommonFactors.md b/src/main/java/leetcode/editor/cn/doc/content/NumberOfCommonFactors.md new file mode 100644 index 0000000..6fe838d --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/NumberOfCommonFactors.md @@ -0,0 +1,28 @@ +

给你两个正整数 ab ,返回 ab 因子的数目。

+ +

如果 x 可以同时整除 ab ,则认为 xab 的一个 公因子

+ +

 

+ +

示例 1:

+ +
输入:a = 12, b = 6
+输出:4
+解释:12 和 6 的公因子是 1、2、3、6 。
+
+ +

示例 2:

+ +
输入:a = 25, b = 30
+输出:2
+解释:25 和 30 的公因子是 1、5 。
+ +

 

+ +

提示:

+ + + +
Related Topics
  • 数学
  • 枚举
  • 数论

  • 👍 28
  • 👎 0
  • \ No newline at end of file