[백준/파이썬] 15234번 풀이
업데이트:
문제 정보
- 문제 출처: 백준 온라인 저지
- 문제 링크: 15234번 문제
- 문제풀이 코드 GitHub 링크
- 제출 언어: Python 3
풀이
문제
Given a sequence of N distinct integer numbers compute the number of pairs that sum to K.
Example:
Given the sequence {1, 2, 3, 4, 5, 6}
-
There are 1 pair of numbers that sums to 3: (1, 2)
-
There are 1 pair of numbers that sums to 4: (3, 1)
-
There are 2 pairs of numbers that sum to 5: (1, 4) and (3, 2)
-
There are 2 pairs of numbers that sum to 6: (5, 1) and (4, 2)
-
There are 3 pairs of numbers that sum to 7: (1, 6), (2, 5) and (3, 4).
-
There are 2 pairs of numbers that sum to 8: (2, 6) and (5, 3) …
입력 요약
The first line will contain two integers N and K.
N represents the number of elements in the sequence and K the goal value.
We want to know how many pairs of numbers sum to K.
The second line will contain N integers separated by spaces.
N <= 1000 …
출력 요약
An integer, the number of pairs that add K.
코드
n,k=map(int,input().split())
l=list(map(int,input().split()))
s=0
for i in range(n-1):
for j in range(i+1,n):
if l[i]+l[j]==k:s+=1
print(s)
설명
핵심은 구현 관점에서 Given a sequence of N distinct integer numbers compute the number of pairs that sum to K.
Example:
Given the sequence {1, 2, 3, 4, 5, 6} …를 만족하도록 로직을 구성하는 것입니다.
코드는 입력을 파싱한 뒤 조건 분기와 계산을 순서대로 수행하고, 문제에서 요구한 형식으로 결과를 출력합니다.
경계값과 예외 케이스도 함께 고려해 오답이 나기 쉬운 상황을 방지합니다.
댓글남기기