输入三个数利用函数获得最大值
时间: 2024-05-05 22:21:13 浏览: 16
可以使用以下代码实现:
```python
def get_max(a, b, c):
max_num = a
if b > max_num:
max_num = b
if c > max_num:
max_num = c
return max_num
a = 5
b = 10
c = 8
max_num = get_max(a, b, c)
print("最大值为:", max_num)
```
在上面的代码中,我们定义了一个名为 `get_max` 的函数,接收三个参数 `a`, `b`, `c`。首先将 `max_num` 的值设置为 `a`,然后逐个比较 `b` 和 `c`,如果有比当前最大值 `max_num` 更大的数,就将 `max_num` 更新为该数。最后返回 `max_num` 的值,即为三个数中的最大值。最后我们输入三个数 `a`, `b`, `c`,调用 `get_max` 函数获取最大值,并将结果打印输出。
相关问题
从键盘输入三个数,利用函数求其最大和最小值
以下是Python代码实现:
```python
def find_max_min(nums):
"""
寻找列表中的最大值和最小值
:param nums: 输入的列表
:return: 返回最大值和最小值
"""
max_num = max(nums)
min_num = min(nums)
return max_num, min_num
# 从键盘输入三个数
a = int(input("请输入第一个数:"))
b = int(input("请输入第二个数:"))
c = int(input("请输入第三个数:"))
# 构建列表
nums = [a, b, c]
# 调用函数求最大值和最小值
max_num, min_num = find_max_min(nums)
# 输出结果
print("最大值为:", max_num)
print("最小值为:", min_num)
```
C语言从键盘输入三个数,利用函数求其最大和最小值
```
#include <stdio.h>
void max_min(int a, int b, int c, int* max, int* min);
int main()
{
int a, b, c;
int max, min;
printf("请输入三个数:\n");
scanf("%d%d%d", &a, &b, &c);
max_min(a, b, c, &max, &min);
printf("最大值为:%d\n", max);
printf("最小值为:%d\n", min);
return 0;
}
void max_min(int a, int b, int c, int* max, int* min)
{
*max = a;
*min = a;
if (b > *max)
*max = b;
if (c > *max)
*max = c;
if (b < *min)
*min = b;
if (c < *min)
*min = c;
}
```
运行结果:
```
请输入三个数:
10 20 30
最大值为:30
最小值为:10
```
相关推荐
![](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)