Home Article Practice 202212-Python STEMA测评

202212-Python STEMA测评

2023-02-20 21:53  views:1185  source:4669    

题目描述
有N(2<=N<=80)堆玻璃球,且已知每堆玻璃球的数量.小明每次只能从其中一堆玻璃球中拿出1颗或者2颗.
请你帮助小明计算出,最少需要拿多少次才能把N堆玻璃球全部拿完.
例如:
N=3,3堆玻璃球的数量分别为6,5,3,最少需要拿8次,具体拿法如下:
6颗玻璃球堆最少需要拿3次.(2+2+2)
5颗玻璃球堆最少需要拿3次.(2+2+1)
3颗玻璃球堆最少需要拿2次.(2+1)
输入
第一行输入一个正整数N(2<=N<=80),表示玻璃球的总堆数
第一行输入N个正整数(1<=正整数<=50),表示每堆玻璃球的数量,正整数之间以一个英文逗号隔开
输出
输出一个正整数,表示拿完N堆玻璃球最少需要拿取的次数
程序:
d=int(input())
s = input() #输入一个字符串,赋值给s
a = s.split(',') #将字符串s按空格拆分,保存到列表a中
lis = [int(i) for i in a]
c=0
for i in lis:
if i%2==0:
c=c+i//2
elif i%2==1:
c=c+i//2+1
print(c)



Disclaimer: The above articles are added by users themselves and are only for typing and communication purposes. They do not represent the views of this website, and this website does not assume any legal responsibility. This statement is hereby made! If there is any infringement of your rights, please contact us promptly to delete it.

字符:    改为:
去打字就可以设置个性皮肤啦!(O ^ ~ ^ O)