首页 > 试题广场 >

记负均正

[编程题]记负均正
  • 热度指数:283455 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}对于给定的 n 个整数 a_1, a_2, \cdots, a_n,统计并计算:
\hspace{23pt}\bullet\,负整数的个数;
\hspace{23pt}\bullet\,正整数的平均值。

输入描述:
\hspace{15pt}第一行输入一个整数 n \left(1 \leqq n \leqq 2 \times 10^3\right) 代表整数的个数。
\hspace{15pt}第二行输入 n 个整数 a_1, a_2, \dots, a_n \left(-10^3 \leqq a_i \leqq 10^3\right) 代表输入的整数。


输出描述:
\hspace{15pt}先输出一个整数,代表负整数的个数;随后在同一行输出一个实数,代表正整数的平均值。

\hspace{15pt}由于实数的计算存在误差,当误差的量级不超过 10^{-6} 时,您的答案都将被接受。具体来说,设您的答案为 a ,标准答案为 b ,当且仅当 \tfrac{|a-b|}{\max(1,|b|)}\leqq 10^{-6} 时,您的答案将被接受。
示例1

输入

6
-1 3 -2 1 0 1

输出

2 1.66666666667
示例2

输入

3
0 0 0

输出

0 0

备注:
\hspace{15pt}本题输出要求已规范,允许细微误差(2025/01/16)。

a,b=int(input()),list(map(int,input().split()))
print(f'{len([i for i in b if i < 0])} {sum([i for i in b if i > 0])/(1 if len([i for i in b if i > 0]) == 0 else len([i for i in b if i > 0]))}')

发表于 2025-02-25 19:31:34 回复(0)
a=int(input())
b=list(map(int,input().split()))
c=[]
d=[]
e=[]
for i in b:
    if i<0:
        c.append(i)
    elif i>0:
        d.append(i)
    elif i==0:
        e.append(i)
if len(c)==0 and len(d)==0:
  print("0 0.0")
else:
  count=len(c)
  sum=0
  for x in d:
    sum=sum+x
    avae=sum/len(d)
  print(count,end=' ')
  print(round(avae,1))
发表于 2023-04-13 14:24:05 回复(0)
n=int(input())
list1=list(map(int,input().split()))
cunt=0
cunt1=0
cunt2=0
for i in list1:
    if i<0:
        cunt+=1
    if i>0:
        cunt1+=i
        cunt2+=1
if cunt2!=0:        
    print(cunt,round(cunt1/cunt2,1),sep=' ')
else:
    print(cunt,round(float(cunt1),2),sep=' ')

发表于 2022-10-04 11:30:44 回复(0)
a=int(input())
b=list(map(int,input().split()))
l=[]
l1=[]
for i in b:
    if i<0:
        l.append(i)
    elif i>0:
        l1.append(i)
if len(l1)==0:
    x=0
else:
    num=0
    for j in l1:
        num=num+j
    x=num/len(l1)
print(len(l),'{:.1f}'.format(x))

发表于 2022-08-31 01:21:32 回复(0)
n = int(input())
li = [int(i) for i in input().split()]
zhengshu = []
fushu = []
for j in li:
    if j > 0:
        zhengshu.append(j)
    elif j < 0:
        fushu.append(j)
print(len(fushu),end=" ")
if len(zhengshu) == 0:
    print("0.0")
else:
    m = sum(zhengshu)/len(zhengshu)
    print("%.1f" % m)
发表于 2022-08-25 18:14:13 回复(0)
n = int(input())
ls = list(map(int,input().split()))
sums = 0
nums = 0
nums1 = ls.count(0)
for i in ls:
    if i > 0:
        sums += i 
    elif i < 0:
        nums += 1
if nums + nums1 == n:
    print(nums,0.0)
else:
    averge = round(sums/(n-nums-nums1),1)
    print(nums,averge,end=' ')

发表于 2022-08-10 19:42:56 回复(0)
while True:
    try:
        n = int(input())
        list1 = input().split(' ')
        # print(n,list1)
        count1 = 0
        count2 = 0
        sum1 = 0
        for i in range(n):
            if list1[i] < '0':
                count1 += 1
            elif list1[i] > '0':
                sum1 += int(list1[i])
                count2 += 1
        if count2 != 0:
            sum1 = sum1/count2
        print(f'{count1} {sum1:.1f}')
    except:
        break


发表于 2022-08-10 16:38:31 回复(0)
n=input()
l=list(map(int,input().split(' ')))
count_fu=0
count_zheng=0
add=0
avg=0
for i in l:
    if i<0:
        count_fu+=1
    elif i>0:
        count_zheng+=1
        add+=i
if count_zheng==0:
    avg=0.0
else:
    avg=add/count_zheng    
print(count_fu,"%.1f"%avg)
发表于 2022-08-08 03:41:43 回复(0)
n = int(input())
val = input().split()
fushu = 0
zhengshu = 0
count = 0
average = 0.0
for i in val:
    if int(i) < 0:
        fushu += 1
    elif int(i) > 0:
        count += 1
        zhengshu += int(i)
        average = round(zhengshu/count,1)
print(fushu,average)
发表于 2022-08-02 22:04:11 回复(0)
n = int(input())
lst = [int(x) for x in input().split(' ')]
s = 0
z = 0
f = 0
for x in lst:
    if x > 0:
        z += 1
        s += x
    elif x < 0:
        f += 1
if not z:
    print('%d %.1f' % (f,s))
else:
    print('%d %.1f' % (f,s/z))

发表于 2022-07-27 18:28:59 回复(0)
n = int(input())
num_lst = list(map(int, input().split()))
pos_num, neg_num, SUM = 0, 0, 0
for i in num_lst:
    if i > 0:
        pos_num += 1
        SUM += i
    elif i < 0:
        neg_num += 1
print(str(neg_num)+' 0.0') if pos_num == 0 else print(str(neg_num)+' '+str(round(SUM/pos_num, 1)))

发表于 2022-07-19 02:11:50 回复(0)
没考虑特殊用例,于是出现了[0,0,0]用例时,提示我0,不能作为除数…… 因为对正整数提取到列表的结果为零,因此该列表在此用例下,len()直接就是0.于是多加了一个判断。还是考虑不够全面
n = int(input())
num = list(map(int,input().split()))
z =[]
f = []
tol = 0
avg = 0
for i in range(len(num)):
    if num[i] >0:
        z.append(num[i])
    elif num[i] <0:
        f.append(num[i])
for j in range(len(z)):
    tol +=z[j]
if len(z) == 0:
    avg =0
elif len(z) !=0:
    avg = tol / len(z)
print(len(f),'%.1f'% (avg))


发表于 2022-07-11 10:32:15 回复(0)
while True:
    try:
        num = int(input())
        lis = list(input().split(' '))
        new_lis = []
        nums = 0
        for i in range(len(lis)):
            if int(lis[i]) < 0:
                nums += 1
            elif int(lis[i]) > 0:
                new_lis.append(int(lis[i]))
            else:
                continue
        print(nums,end=' ')
        if len(new_lis) > 0:
            print(round(sum(new_lis)/len(new_lis),1)) 
        else:
            print('0.0')
    except:
        break
发表于 2022-06-26 12:06:09 回复(0)
while True:
    try:
        num = int(input())
        nums = list(map(int, input().split()))
        count = 0
        positive_nums = []
        for number in nums:
            if number < 0:
                count += 1
            elif number > 0:
                positive_nums.append(number)
        if len(positive_nums) > 0:
            positive_avg = sum(positive_nums)/len(positive_nums)
            print(count, round(positive_avg, 1))
        else:
            print(count, 0.0)
    except:
        break
发表于 2022-06-25 11:30:42 回复(0)
正整数个数为0的时候,不可以作分母,所以要分开讨论
while True:
    try:
        n=int(input())
        lst=list(map(int,input().split()))
        a,b=[],[]
        for i in lst:
            if i>0:
                a.append(i)
            elif i<0:
                b.append(i)
        if len(a)>0:
            print(len(b),round(sum(a)/len(a),1))
        else:
            print(len(b),'0.0')
    except:
        break

发表于 2022-06-18 17:16:13 回复(0)
while True:
    try:
        s = int(input())
        ss = map(int,input().split())
        s1 = []
        s2 = 0
        for i in ss:
            if i > 0:
                s1.append(i)
            elif i < 0:
                s2 += 1
        if s1 == []:
            s3 = 0
        else:
            s3 = sum(s1)/len(s1)          
        print("%d %.1f" % (s2,s3))            
    except:
        break
发表于 2022-06-06 20:57:07 回复(0)
def main():
    while True:
        try:
            n=int(input().strip())
            task=list(map(int,input().strip().split(' ')))
            task_right=[item for item in task if item >0]
            task_left=[item for item in task if item <0]
            print(len(task_left),end=' ')
            if len(task_right)==0:
                print(0.0)
            else:
                aver=sum(task_right)/len(task_right)
                print(f'{aver:.1f}')
        except EOFError as e:
            break

main()
发表于 2022-06-05 12:11:14 回复(0)
while True:
    try:
        s0, s1 = int(input()), [int(x) for x in input().split()]
        s00 = [x for x in s1 if int(x) < 0]
        s2 = [x for x in s1 if int(x) > 0 ]
        if s2:
            print(f'{len(s00)} {round(sum(s2)/len(s2), 1)}')
        else:
            print(f'{len(s00)} {float(0.0)}')
    except:
        break

发表于 2022-05-31 13:31:15 回复(0)
n=int(input())
data=input().split(' ')
n1=0
n2=0
n3=0
toal=0
for num in data:
    if int(num)<0:
        n1=n1+1
    if int(num)>0:
        toal=toal+int(num)
        n2=n2+1
    if int(num)==0:
        n3=n3+1
if n2>0:
    toal=toal/(n2)
else:
    toal=0.0
print(n1,round(toal,1))

发表于 2022-05-25 10:50:09 回复(0)
n = int(input())  # 正整数n
str1 = input()  # n个整数
if 0 <= n < 2000:
    count1 = 0
    sum1 = 0
    count2 = 0
    list1 = list(str1.split())  # 以空格字符串劈分str1并且转换为列表类型
    for i in list1:
        if int(i) == 0:
            pass
        elif int(i) < 0:
            count1 = count1 + 1
        else:
            sum1 = sum1 + int(i)
            count2 = count2 + 1
    if sum1 == 0:
        print(count1, 0.0)  # 输出一位小数
    else:
        print(count1, "{0:.1f}".format(sum1 / count2))  # 输出一位小数
发表于 2022-05-22 20:48:24 回复(0)

问题信息

难度:
53条回答 25588浏览

热门推荐

通过挑战的用户

查看代码
记负均正