给定一系列正整数,请按要求对数字进行分类,并输出以下 5 个数字:
-
A
1
A1
= 能被 5 整除的数字中所有偶数的和; -
A
2
A2
= 将被 5 除后余 1 的数字按给出顺序进行交错求和,即计算 n1 −n2 +n3 −n4⋯; -
A
3
A
3
= 被 5 除后余 2 的数字的个数; -
A
4
A
4
= 被 5 除后余 3 的数字的平均数,精确到小数点后 1 位; -
A
5
A5
= 被 5 除后余 4 的数字中最大数字。
输入格式:
每个输入包含 1 个测试用例。每个测试用例先给出一个不超过 1000 的正整数 N,随后给出 N 个不超过 1000 的待分类的正整数。数字间以空格分隔。
输出格式:
对给定的 N 个正整数,按题目要求计算
A
1
A
1
~
A
5
A
5
并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。
若其中某一类数字不存在,则在相应位置输出
N
。
输入样例 1:
13 1 2 3 4 5 6 7 8 9 10 20 16 18
输出样例 1:
30 11 2 9.7 9
输入样例 2:
8 1 2 4 5 6 7 9 16
输出样例 2:
N 11 2 N 9
代码如下
def classify():
content = map(int, raw_input().strip().split(' '))
A = []
result = []
for i in range(5):
A.append([])
for i in range(0, content[0]):
remainder = content[i+1] % 5
A[remainder].append(content[i+1])
for i in range(5):
if len(A[i]) == 0:
result.append('N')
else:
result.append(0)
evenNum = 0
for j in range(len(A[i])):
if i == 0:
if A[i][j] % 2 == 0:
result[i] += A[i][j]
evenNum += 1
elif (j == len(A[i]) - 1) and (evenNum == 0):
result[i] = 'N'
elif i == 1:
if j % 2 == 0:
result[i] += A[i][j]
else:
result[i] -= A[i][j]
elif i == 2:
result[i] += 1
elif i == 3:
if j == len(A[i]) - 1:
result[i] = round(float(result[i] + A[i][j]) / len(A[i]), 1)
else:
result[i] += A[i][j]
elif i == 4:
result[i] = max(result[i], A[i][j])
print (' ').join(map(str, result))
if __name__ == '__main__':
classify()