Title Description
Input int Type of the array , Ask if the array can be divided into two groups , Make the sum of the elements in the two groups equal , also , all 5 Multiple of must be in one of the groups , all 3 In another group ( barring 5 Multiple ), Can meet the above conditions , Output true; Output when not satisfied false.
This question contains several groups of sample input .
Input description :
The first row is the number of data , The second line is the input data
Output description :
return true perhaps false
Example 1
Input
4
1 5 -5 1
3
3 5 8
Output
true
explain
The first example :
The first group :5 -5 1
The second group :1
The second example : because 3 and 5 Can't be put in the same group , So there is no such thing as a division .
The code implementation is as follows :
def func():
while True:
try:
n = int(input())
a = list(map(int,input().split()))
s5,s3,s0= [],[],[]
for i in a:
if i%5 ==0:
s5.append(i)
elif i%3 ==0:
s3.append(i)
else:
s0.append(abs(i))
s0.sort(reverse=True)
sum5 = sum(s5)
sum3 = sum(s3)
#print(s3)
for i in s0:
if sum5 < sum3:
sum5 +=i
else:
sum3 +=i
if sum5 == sum3:
print('true')
else:
print('false')
except Exception as e:
#print(e)
break
if __name__ == '__main__':
func()