[백준/파이썬] 14542번 풀이

업데이트:



문제 정보


풀이

문제

You are to find the sum of the outer number of an isosceles right triangle.

For example, for n = 5 the isosceles right triangle grid are filled with non-negative integers that are less than 100:

5 1 8 9 6 1 2 7 2 6 3 5 7 8 9

The sums of the outer integers are calculated as below:

sum = 5 + 1 + 9 + 2 + 3 + 5 + 7 + 8 + 9 + 6 + 1 + 8 = 64

입력 요약
The input consists of a few test cases. For each test case, the first line of input is a positive integer n (n <= 10) that determines the dimension of the triangle. Each of the next n lines contains 1 to n integers respectively that will fill the isosceles right triangle. …

출력 요약
Each line of output will start with “Case #:” where # is replaced by the case number. Then you have to output the sum of the outer numbers of the triangle.

코드

t=1
while True:
    n=int(input())
    if n==0:break
    l=[list(map(int,input().split()))for _ in range(n)]
    s=l[0][0]
    if n>1:s+=sum(l[-1])
    for i in range(1,n-1):s+=l[i][0]+l[i][-1]
    print(f'Case #{t}:{s}')
    t+=1

설명

핵심은 구현 관점에서 You are to find the sum of the outer number of an isosceles right triangle. …를 만족하도록 로직을 구성하는 것입니다.

코드는 입력을 파싱한 뒤 조건 분기와 계산을 순서대로 수행하고, 문제에서 요구한 형식으로 결과를 출력합니다.

경계값과 예외 케이스도 함께 고려해 오답이 나기 쉬운 상황을 방지합니다.



다음 읽을거리

관련 허브 페이지에서 같은 주제의 글을 이어서 확인할 수 있습니다.

댓글남기기