Code:
scorelist = []
for i in range(10):
scorelist.append(int(input()))
score = sum(scorelist)
if score <=100:
print(score)
else:
score2 = 0
for i in range(9,-1,-1):
if score>100:
score -= scorelist[i]
elif score ==100:
break
else:
score2 = score+scorelist[i+1]
break
if score ==100:
print(100)
else:
if 100-score >=score2 -100:
print(score2)
else:
print(score)
In this code:
- score is the number that is smaller and closest to 100
- score2 is the number that is higher and closest to 100
To explain the question, there are 10 mushrooms in front of Super Mario in a row and if he eats the mushroom, he gets a certain point. He has to get the mushrooms in order. If the point of mushroom is inputed, the score has to get the closest to a 100.
We first get the input and get the total score possible by adding them.
scorelist = []
for i in range(10):
scorelist.append(int(input()))
score = sum(scorelist)
If the total score is a 100, we make the code print 100 as a score.
if score <=100:
print(score)
But if it isn't a 100, we search the mushroom points in a reversed order because there are higher scores in the right side. And if the score is higher than 100, we minus the mushroom point. If the score reaches exactly a 100, we break out.
else:
score2 = 0
for i in range(9,-1,-1):
if score>100:
score -= scorelist[i]
elif score ==100:
break
We compare the two numbers and see which number is closer to 100. If they both have the same value of numbers left between it and 100, we choose the number that is higher.
else:
if 100-score >=score2 -100:
print(score2)
else:
print(score)