[백준/파이썬] 16316번 풀이
업데이트:
문제 정보
- 문제 출처: 백준 온라인 저지
- 문제 링크: 16316번 문제
- 문제풀이 코드 GitHub 링크
- 제출 언어: Python 3
풀이
문제
Arild just turned 1 year old, and is currently learning how to count. His favorite thing to count is how many mouthfuls he has in a meal: every time he gets a bite, he will count it by saying the number out loud.
Unfortunately, talking while having a mouthful sometimes causes Arild to mumble incomprehensibly, making it hard to know how far he has counted. Sometimes you even suspect he loses his count! You decide to write a program to determine whether Arild’s counting makes sense or not.
입력 요약
The first line of input contains an integer n (1 ≤ n ≤ 1 000), the number of bites Arild receives. Then second line contains n space-separated words spoken by Arild, the i’th of which is either a non-negative integer ai (0 ≤ ai ≤ 10 000) or the string “mumble”.
출력 요약
If Arild’s counting might make sense, print the string “makes sense”. Otherwise, print the string “something is fishy”.
코드
n=int(input())
l=input().split()
for i in range(n):l[i]=i+1 if l[i]=='mumble'else int(l[i])
print('makes sense' if sum(l)==n*(n+1)//2 else'something is fishy')
설명
핵심은 구현 관점에서 Arild just turned 1 year old, and is currently learning how to count. …를 만족하도록 로직을 구성하는 것입니다.
코드는 입력을 파싱한 뒤 조건 분기와 계산을 순서대로 수행하고, 문제에서 요구한 형식으로 결과를 출력합니다.
경계값과 예외 케이스도 함께 고려해 오답이 나기 쉬운 상황을 방지합니다.
댓글남기기