程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[computer test questions (implementation language: python3)] array grouping

編輯:Python

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()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved