输入两个正整数m和n(2<=m和n<=100000),计算它们的最大公约数和最小公倍数并输出。 输入格式: 在一行中输入两个整数m和n,2<=m<=100000,2<=n<=100000 输出格式: 最大公约数x 最小公倍数y
时间: 2024-02-17 17:03:41 浏览: 60
好的,我的回答如下:
```python
m, n = map(int, input().split())
# 求最大公约数
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
# 求最小公倍数
def lcm(a, b):
return a * b // gcd(a, b)
print(gcd(m, n), lcm(m, n))
```
希望能够帮到你!
相关问题
输入两个正整数m和n,求其最大公约数和最小公倍数,其中最小公倍数的计算公式为:最小公倍数=m*n/最大公约数 输入 输入两个正整数m和n (1<=n,m<=10000) 输出 输出其最大公约数和最小公倍数
可以使用辗转相除法来求解最大公约数,具体实现如下:
```c
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int main() {
int m, n;
scanf("%d %d", &m, &n);
int g = gcd(m, n);
int l = m * n / g;
printf("%d %d\n", g, l);
return 0;
}
```
其中,`gcd`函数使用递归实现辗转相除法求解最大公约数,`main`函数中先读入两个正整数m和n,然后调用`gcd`函数求解最大公约数g,最后根据最小公倍数的计算公式计算出最小公倍数l并输出。
用Java语言编写代码:输入两个正整数m和n(2<=m和n<=100000),计算它们的最大公约数和最小公倍数并输出。
好的,以下是Java代码实现:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
int gcd = getGcd(m, n);
int lcm = getLcm(m, n, gcd);
System.out.println(gcd + " " + lcm);
}
// 求最大公约数
private static int getGcd(int a, int b) {
if (b == 0) {
return a;
} else {
return getGcd(b, a % b);
}
}
// 求最小公倍数
private static int getLcm(int a, int b, int gcd) {
return a * b / gcd;
}
}
```
代码思路解析:
1. 首先通过 `Scanner` 类从控制台输入两个正整数 `m` 和 `n`,并存储到对应的变量中。
2. 调用 `getGcd` 方法求最大公约数,并将结果存储到 `gcd` 变量中。
3. 调用 `getLcm` 方法求最小公倍数,并将结果存储到 `lcm` 变量中。
4. 最后输出最大公约数和最小公倍数。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)