B1012 数字分类(python)

在这里插入图片描述
在这里插入图片描述
version1:
有一个测试点没过

这样写很臃肿……

过程中的问题:
思路要清晰,想好了再写,一些计数变量的关系别弄错

nums = input().split(' ')

N = nums[0]
nums.remove(nums[0])

def A1(ls):
    cal = 0
    for s in ls:
        if eval(s) % 2 == 0 and eval(s) % 5 == 0:
            cal += eval(s)
    if cal:
        return cal
    else:
        return 'N'

def A2(ls):
    cal = 0
    cnt = 0
    for i in range(len(ls)):
        if eval(ls[i]) % 5 ==1:
            cal += ((-1) ** cnt) * eval(ls[i])
            cnt += 1
    if cal:
        return cal
    else:
        return 'N'

def A3(ls):
    cnt = 0
    for s in ls:
        if eval(s) % 5 == 2:
            cnt += 1
    if cnt:
        return cnt
    else:
        return 'N'

def A4(ls):
    cnt = 0
    av = 0
    for s in ls:
        if eval(s) % 5 == 3:
            cnt += 1
            av += eval(s)
    if av:
        return round(av / cnt,1)
    else:
        return 'N'

def A5(ls):
    m = 0
    for s in ls:
        if eval(s) % 5 == 4 and eval(s) > m:
            m = eval(s)
    if m:
        return m
    else:
        return 'N'

print(A1(nums),A2(nums),A3(nums),A4(nums),A5(nums))

version2:
AC

N, *nums = map(int, input().split(' '))
lis1, lis2, cnt, lis4, lis5 = [], [], 0, [], []
for num in nums:
    if num % 5 == 0 and num % 2 == 0:
        lis1.append(num)
    elif num % 5 == 1:
        lis2.append(num)
    elif num % 5 == 2:
        cnt += 1
    elif num % 5 == 3:
        lis4.append(num)
    elif num % 5 == 4:
        lis5.append(num)
total = 0
for i in range(len(lis2)):
    total += ((-1)**i)*lis2[i]
print('N' if lis1 == [] else sum(lis1),
      'N' if lis2 == [] else total,
      'N' if cnt == 0 else cnt,
      'N' if lis4 == [] else '%.1f'%(sum(lis4)/len(lis4)),
      'N' if lis5 == [] else max(lis5))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值