输入n个整数,统计正数、负数、零的个数,并输出结果
时间: 2024-05-12 21:20:57 浏览: 87
示例:
输入:5 -1 3 0 -9
输出:正数个数:2,负数个数:2,零的个数:1
代码实现:
```python
nums = input().split() # 输入n个整数,并以空格分隔
positive = 0 # 正数个数
negative = 0 # 负数个数
zero = 0 # 零的个数
for num in nums:
num = int(num) # 将输入的字符串转为整数
if num > 0:
positive += 1
elif num < 0:
negative += 1
else:
zero += 1
print("正数个数:{},负数个数:{},零的个数:{}".format(positive, negative, zero))
```
相关问题
输入n和n个整数求正数负数和零的个数
以下是Python代码:
```python
n = int(input())
nums = list(map(int, input().split()))
positive_count = 0
negative_count = 0
zero_count = 0
for num in nums:
if num > 0:
positive_count += 1
elif num < 0:
negative_count += 1
else:
zero_count += 1
print("positive: {}".format(positive_count))
print("negative: {}".format(negative_count))
print("zero: {}".format(zero_count))
```
输入示例:
```
5
1 2 0 -3 -4
```
输出示例:
```
positive: 2
negative: 2
zero: 1
```
c语言输入n和n个整数求正数负数和零的个数
以下是一个使用数组的C语言程序,可以输入n和n个整数,然后计算正数、负数和零的个数:
```
#include <stdio.h>
int main() {
int n, i, num, pos = 0, neg = 0, zero = 0;
printf("请输入n的值:");
scanf("%d", &n);
int arr[n];
printf("请输入%d个整数:", n);
for (i = 0; i < n; i++) {
scanf("%d", &num);
arr[i] = num;
if (num > 0) {
pos++;
} else if (num < 0) {
neg++;
} else {
zero++;
}
}
printf("正数个数:%d\n负数个数:%d\n零的个数:%d\n", pos, neg, zero);
return 0;
}
```
程序首先提示用户输入n的值,然后创建一个大小为n的整数数组。接下来,程序提示用户输入n个整数,并逐个将它们存储在数组中。在这个过程中,程序会计算正数、负数和零的个数,最后输出这些数字。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)