首页 > 试题广场 >

统计数据正负个数

[编程题]统计数据正负个数
  • 热度指数:26629 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
输入10个整数,分别统计输出正数、负数的个数。

输入描述:
输入10个整数(范围-231~231-1),用空格分隔。


输出描述:
两行,第一行正数个数,第二行负数个数,具体格式见样例。
示例1

输入

-1 2 3 -6 7 8 -1 6 8 10

输出

positive:7
negative:3
a,b,c,d,e,f,g,h,i,j=map(int,input().split())
x=list((a,b,c,d,e,f,g,h,i,j))
count=0
zount=0
for i in x:
    if int(i)<0:
        count+=1
    else:
        zount+=1
print(f"positive:{zount}")
print(f"negative:{count}")
发表于 2024-08-25 17:20:17 回复(0)
nums = list(map(int,input().split(" ")))
print(f"positive:{len([i for i in nums if i >= 0])}\nnegative:{len([i for i in nums if i < 0])}")
编辑于 2024-04-13 17:55:49 回复(0)
list统计元素
a = list(map(int,input().split()))
p = list(i for i in a if i > 0)
n = list(i for i in a if i < 0)
print("positive:" + str(len(p)))
print("negative:" + str(len(n)))


编辑于 2024-02-04 13:07:21 回复(0)
num=list(map(int,input().split()))
p=0
n=0
for i in num:
    if i>=0:
        p+=1
    else:
        n+=1  
print('positive:{}'.format(p))
print('negative:{}'.format(n))   

发表于 2023-01-18 10:08:45 回复(0)
l=list(map(int,input().split(' ')))# 将输入转化为列表
a=0
b=0
for i in l:# 定义i
        if i>=0:
            a+=1# 代表正数个数
        else:
            b+=1
print("positive:{}".format(a))# 这样输出的格式就是正确的,print("positive:",a)输出格式带有空格
print("negative:{}".format(b))
发表于 2022-05-01 21:53:01 回复(0)
ln = map(int, input().split())
positive = 0
negative = 0
for i in ln:
    if i < 0:
        negative += 1
    else:
        positive += 1
print("positive:{}\nnegative:{}".format(positive,negative))
发表于 2021-08-25 14:18:34 回复(0)