编写一个程序,输入三个整数,输出其中最小值,并判断其是偶数还是奇数
时间: 2024-02-22 15:59:50 浏览: 31
好的,我可以帮你写一个 Python 程序来完成这个任务。代码如下:
```python
a = int(input("请输入第一个整数:"))
b = int(input("请输入第二个整数:"))
c = int(input("请输入第三个整数:"))
min_num = min(a, b, c)
print("最小值为:", min_num)
if min_num % 2 == 0:
print("最小值为偶数")
else:
print("最小值为奇数")
```
这个程序首先通过 `input` 函数获取用户输入的三个整数,然后使用 `min` 函数获取其中的最小值并进行输出。接着,使用取余运算符 `%` 判断最小值是否为偶数,并输出相应的结果。
希望这个程序能够帮助到你!
相关问题
从键盘输入10个两位整数,存放到列表中,然后输出列表各元素; 将10个整数按升序排序输出,并输出其中的最大值和最小值; 将10个整数中的奇数和偶数分别存放在两个列表中,并统计奇数和偶数的个数,然后将奇数和偶数分别按降序排序输出。
下面是实现上述功能的Python代码:
```
# 从键盘输入10个两位整数,并存放到列表中
num_list = []
for i in range(10):
num = int(input("请输入一个两位整数:"))
num_list.append(num)
# 输出列表各元素
print("列表中的元素为:", num_list)
# 将10个整数按升序排序输出,并输出其中的最大值和最小值
num_list.sort()
print("升序排序后的列表为:", num_list)
print("最大值为:", max(num_list))
print("最小值为:", min(num_list))
# 将10个整数中的奇数和偶数分别存放在两个列表中,并统计奇数和偶数的个数
odd_list = []
even_list = []
odd_count = 0
even_count = 0
for num in num_list:
if num % 2 == 0:
even_list.append(num)
even_count += 1
else:
odd_list.append(num)
odd_count += 1
# 将奇数和偶数分别按降序排序输出
odd_list.sort(reverse=True)
even_list.sort(reverse=True)
print("奇数列表为:", odd_list)
print("偶数列表为:", even_list)
print("奇数个数为:", odd_count)
print("偶数个数为:", even_count)
```
运行代码后,可以按照提示输入10个两位整数,然后程序会输出列表各元素、升序排序后的列表、最大值和最小值、奇数列表、偶数列表、奇数个数和偶数个数。
编写程序,利用循环产生16个随机二位正整数, 再将16位随机正整数中 偶数存放在even列表中,奇数存放在odd列表中, 然后分别将even和odd列表打印。 最后输出odd列表的最大值、最小值和所有元素之和。
好的,我可以帮您解决这个编程问题。
以下是 Python 代码:
```python
import random
even = []
odd = []
for i in range(16):
num = random.randint(10, 99)
if num % 2 == 0:
even.append(num)
else:
odd.append(num)
print("even list:", even)
print("odd list:", odd)
print("max element in odd list:", max(odd))
print("min element in odd list:", min(odd))
print("sum of all elements in odd list:", sum(odd))
```
上述代码利用 `random` 模块生成 16 个随机二位正整数,并使用条件语句将偶数和奇数分别存入 `even` 和 `odd` 列表中。然后分别打印这两个列表。最后使用 Python 的内置函数 `max()`、`min()` 和 `sum()` 分别输出 `odd` 列表的最大值、最小值和所有元素之和。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)