hi jaeneee
baekjoon(2851-슈퍼 마리오)_ python 본문
1) 문제

2) 예시


3) 제출
cnt = plus = small = big = 0
for i in range(10):
plus += int(input())
if cnt < 1 and plus >= 100:
big = plus
break
elif plus < 100:
small = plus
if big == 0:
print(small)
elif (100 - small) < (big - 100):
print(small)
else:
print(big)
4) 마무리

8번 시도,, ---> big이 0일 경우 고려X
break를 써서 중간에 빠져나와도 된다는 것을 나중에 알았다,,
조금 더 간단하게 하면
plus = small = big = 0 //전부 0으로 초기화
for i in range(10): //10개의 숫자 받기
plus += int(input()) //10개의 숫자를 따로 저장하지 않고 plus에 더하기
if plus >= 100: //100보다 클 경우, big에 저장하고 반복문 탈출
big = plus
break
elif plus < 100: //100보다 작을 경우, small에 저장
small = plus
if (100 - small) < (big - 100) or big == 0: //big이 없을 경우(다 더해도 100이 넘지 않을 경우
print(small) //small이 big보다 100에 가까울 경우에 small 출력
else: //big이 100에 더 가까울 경우에 big 출력
print(big)
이렇게 elif 문을 하나 줄일 수 있고 cnt도 없앨 수 있다.
(처음에 cnt를 썼던 이유는 빠져나오지 않고 10개의 숫자를 다 받아야 하는 줄 알았다.)
파이썬에서는 &&나 || 대신 and나 or을 쓸 수 있다는 것을 알았다.
'알고리즘 > baekjoon' 카테고리의 다른 글
baekjoon(2941-크로아티아 알파벳)_ python (0) | 2022.04.13 |
---|---|
baekjoon(11399-ATM)_ python (0) | 2022.03.23 |
baekjoon(2460-지능형 기차 2)_ python (0) | 2022.03.23 |
baekjoon(10871-X보다 작은 수)_ python (0) | 2022.03.16 |
baekjoon(2439-별 찍기-2)_ python (0) | 2022.03.16 |