题解 | #记负均正II#
记负均正II
http://www.nowcoder.com/practice/64f6f222499c4c94b338e588592b6a62
#解题思路,使用while和try不定长循环输入,输入之后做判断是负数还是正数,并用全局变量来计数,设置一个num_sum来累加正数,最终用num_sum除以正数的计数得到平均值,要注意除数为0要跳过运算,否则会出错
num_sum = 0
f_c = 0
z_c = 0
while True:
try:
input_number = int(input())
if input_number < 0:
f_c += 1
elif input_number > 0:
z_c += 1
num_sum += input_number
else:
pass
except Exception:
break
if z_c != 0:
out_put = round((num_sum / z_c), 1)
print(f_c)
if z_c == 0:
print("0.0")
else:
print(out_put)