输入 n 个整型数,统计其中的负数个数并求所有非负数的平均值,结果保留一位小数,如果没有非负数,则平均值为0
本题有多组输入数据,输入到文件末尾。
数据范围: ,其中每个数都满足
#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'))#求平均,保留小数点后一位
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'))
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
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)
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))