用Python表示设计递归函数,求两个数的最大公约数。
时间: 2024-02-12 11:08:52 浏览: 24
可以使用以下代码来实现:
```python
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
```
其中,`a`和`b`分别为两个输入的数字。如果`b`为0,则直接返回`a`,否则继续递归调用`gcd`函数,将`b`和`a % b`作为参数传入。这里使用了Python中的求余运算符`%`来计算`a`除以`b`的余数,并作为下一次递归调用的第二个参数。最终,当`b`为0时,最大公约数即为`a`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)