首页 > 试题广场 >

记负均正II

[编程题]记负均正II
  • 热度指数:226160 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
输入 n 个整型数,统计其中的负数个数并求所有非负数的平均值,结果保留一位小数,如果没有非负数,则平均值为0
本题有多组输入数据,输入到文件末尾。

数据范围: ,其中每个数都满足

输入描述:

输入任意个整数,每行输入一个。



输出描述:

输出负数个数以及所有非负数的平均值

示例1

输入

-13
-4
-7

输出

3
0.0
示例2

输入

-12
1
2

输出

1
1.5
#import numpy as np
a=[]#记录正整数元素
counter1 = 0#记录负数个数
counter2 = 0#记录正数个数
while True:
    try:
        str1 = int(input()) #转为int
        if str1 > 0:#为+时
            a.append(str1)#添加入列表
            counter2 = counter2 +1#正数数量+1
        else:
            counter1 =counter1+1#负数数量+1
    except:
        break
print(counter1)#打印负数数量
if len(a) == 0:#无正数时
    print(0.0)
else:
    #a=np.mean(a)
    print(format(sum(a)/counter2,'.1f'))#求平均,保留小数点后一位

发表于 2021-07-01 18:29:10 回复(0)
a=[]
b=[]
while True:
    try:
        n=int(input())
        if n<0:
            a.append(n)
        else:
            b.append(n)
    except EOFError:
        break

print(len(a))
print(round(sum(b)/len(b),1) if b else '0.0')


求赞赞,么么哒

发表于 2021-06-15 09:33:02 回复(0)
a = []
sum = 0
count_p = 0
count_n = 0
while True:
    try:
        a = int(input())
        if a >= 0:
            sum += a
            count_p += 1
        if a< 0:
            count_n += 1
    except:
        break
if count_p == 0:
    print(count_n)
    print(0)
else:
    ave = sum/count_p
    ave_1 = int(ave*10)/10 
    if ave - ave_1 >= 0.05:
        ave_1 = (int(ave * 10) + 1) / 10  # 重置为保留一位小数
    print(count_n)
    print(ave_1)
发表于 2021-03-27 17:16:07 回复(0)
ints = []
while True:
    try:
        ints.append(int(input()))
    except EOFError:
        break
nums_pos = list(filter(lambda x: x>=0, ints))
nums_neg = list(filter(lambda x: x<0, ints))
if len(nums_neg) == len(ints):
    print(len(nums_neg))
    print(0.0)
else:
    print(len(nums_neg))
    print(format(sum(nums_pos)/len(nums_pos), '.1f'))

发表于 2021-03-24 16:49:07 回复(0)
import sys


neg_count, non_neg_count, non_neg_sum = 0, 0, 0
for i in sys.stdin:
    i = int(i)
    if i < 0:
        neg_count += 1
    else:
        non_neg_count += 1
        non_neg_sum += i

ave = "{:.1f}".format(0 if not non_neg_count else non_neg_sum/non_neg_count)
print(neg_count)
print(ave)

发表于 2020-12-19 17:26:19 回复(0)
import sys
strList = []
for line in sys.stdin:  #没有当接受到输入结束信号就一直遍历每一行
    tempStr = line.split()#对字符串利用空字符进行切片
    strList.extend(tempStr)#
b=[]
c=[]
for i in strList:
    if int(i) >= 0:#zhengshu
        c.append(int(i))
    else:#fushu
        b.append(int(i))
print(len(b))
if len(c)== 0:
    print("0.0")
else:
    print(round(sum(c)/len(c),1))
发表于 2020-12-13 11:05:09 回复(0)
这段代码到底哪里除了问题
while True:
    try:
        count1=0
        count2=0
        num=0
        a=int(input().split())
        for i in a:
            if i<0:
                count1+=1
            else:
                num+=i
                count2+=1
        print(count1)
        print(round(num/count2,1))
    except:
        break
编辑于 2020-10-30 17:02:27 回复(0)
现在是多行输入了


a, pos, neg = [], [], []

while True:
    try:
        num = int(input())
        a.append(num)
    except:
        break

for i in a:
    neg.append(int(i)) if int(i) < 0 else pos.append(int(i))
print(len(neg))
print(round(sum(pos) / len(pos), 1) if pos else "0.0")


发表于 2020-10-12 00:03:01 回复(0)
negative, no_negative, no_negative_sum = 0, 0, 0
num = input()
while num:
    try:
        num = int(num)
        if num < 0:
            negative += 1
        else:
            no_negative += 1
            no_negative_sum += num
        num = input()
    except:
        print(negative)
        if no_negative == 0:
            print(0.0)
        else:
            print('{:.1f}'.format(no_negative_sum / no_negative))
        break
        


编辑于 2020-10-22 18:23:41 回复(0)
看前面有人说这道题是一行输入,现在好像又变成多行了。
ans = 0
num = 0
neg = 0
while True:
    try:
        n = int(input())
        if  n > 0:
            ans += n
            num += 1
        else:
            neg += 1
    except EOFError:
        break
print(neg)
print(round(ans/num, 1)) if num > 0 else print(0.0)

编辑于 2020-09-09 11:26:04 回复(2)
import sys
a = list(map(lambda x:x.strip(), sys.stdin.readlines()))
res=0
pos = []
for i in a:
    if int(i) < 0:
        res+=1
    else:
        pos.append(int(i))
print(res)
print(round(sum(pos) / len(pos),1) if pos else '0.0')
发表于 2020-09-05 17:26:32 回复(0)
这道题怎么不把输入值的方式说清楚?垃圾题
发表于 2020-09-03 14:31:49 回复(0)
numbers = int(input("Please enter intergers:"))
negative_num = 0
positive_num = []
while 
for number in numbers:
    if number < 0:
        negative_num += 1
    else:
        current_num = number
        positive_num.append()

positive_avage = sum(positive_num) / len(positive_num)
        

        

发表于 2020-09-01 15:19:11 回复(0)

def solution(c1, c2, s):
print(c1)
print("%.1f"%(s/c2))
if name == 'main':
c1, c2, s = 0, 0, 0
while True:
try:
a = int(input())
if a < 0:
c1 += 1
else:
c2 += 1
s += a
except:
break
solution(c1, c2, s)

发表于 2020-08-13 16:10:23 回复(0)
这样为什么不行
nums = [int(each) for each in input().split()]
count,num_positive = 0, []
for num in nums:
    if num < 0:
        count = count + 1
    else:
        num_positive.append(num)
print(count)
print(round(sum(num_positive)/(len(nums) - count), 1))



发表于 2020-08-11 23:23:23 回复(0)
s = input()
num = [int(n) for n in s.split()]
neg = 0
pos = []
for i in num:
    if i < 0:
        neg += 1
    else:
        pos.append(i)
print(neg)
if len(pos) == 0:
    print('%.1f' % 0)
else:
    print('{:.1f}'.format(sum(pos)/len(pos)))
发表于 2020-07-15 18:45:10 回复(0)
l=list(map(int,input().split()))
count=0
countp=0
sum=0
for i in l:
if i<0:
count+=1
else:
countp+=1
sum+=i
print(count)
if countp==0:
print('0.0')
else:
print(format((sum/countp),'.1f'))
输入不是很多列
如果是很多列可以用这个
try:
num=1
count=0
total=0
a=int(input())
while(type(a) is int):
if a<0:
count=count+1
if a>=0:
total=total+a
a=int(input())
num=num+1

except:
average=total/(num-count)
print(format(average,".1f"))


编辑于 2020-06-21 12:28:57 回复(0)
u=list(map(int,input().split()))
a=0
b=0
for i in u:
    if i>=0:
        b=b+i
    elif i<0:
        a=a+1   
c=b/(len(u)-a)
print('''%d
%.1f'''%(a,c))
发表于 2020-04-19 11:28:07 回复(0)
while True:
    try:
        from functools import reduce
        num = list(map(int,input().split()))
        count = 0
        result = []
        for i in num:
            if i < 0:
                count += 1
            elif i > 0:
                result.append(i)
        print(count)
        print(round((reduce(lambda x,y:x+y,result)/len(result)),1))
    except:
        break

发表于 2020-03-31 15:34:26 回复(0)

问题信息

难度:
32条回答 35275浏览

热门推荐

通过挑战的用户

查看代码