将三个数由小到大输出的函数
时间: 2024-10-10 16:06:47 浏览: 31
在编程中,你可以创建一个函数,用于接收三个数作为输入,然后通过某种排序算法将它们按照从小到大的顺序返回。这里提供一个简单的Python示例,使用内置的`min()`和`max()`函数:
```python
def sort_three_numbers(a, b, c):
# 先找出最小值
min_value = min(a, b, c)
# 如果最小值不是最大值,再找出剩余两个数的最大值
if min_value != max(a, b, c):
max_value = max(min_value, max(a, b, c))
else:
max_value = max_value
return (min_value, max_value)
# 示例调用
numbers = 5, 2, 8
sorted_numbers = sort_three_numbers(*numbers)
print("Sorted numbers:", sorted_numbers)
```
这个函数首先找到最小值,如果它不是最大的,那么就找出剩下的两个较大的数。注意,这里假设三个数都是整数或可以比较大小的其他类型。
阅读全文
相关推荐
















