RUBY

[백준] 수들의 합2 본문

PS/BOJ

[백준] 수들의 합2

RUBY_루비 2020. 9. 17. 23:59

출처:: www.acmicpc.net/problem/2003

분류:: 투포인터

 

1. 문제 이해 및 해결과정

 

2. 풀이방법

  1. python

#수들의 합 2
#https://www.acmicpc.net/problem/2003
import sys
sys.stdin = open("input.txt","r")
n,m=map(int,input().split())
arr=list(map(int,input().split()))
s,e=0,0
cnt=0
sum=0
for s in range(n):

    while sum<m and e<n:#합이 m보다 작고, 끝점이 n보다 작으면
        sum+=arr[e]
        e+=1
    if sum==m: #부분합과 같으면
        cnt+=1
    #합이 m보다 크면
    sum-=arr[s]
print(cnt)

 

3. 오답원인

 

4. 알게된 점

 

'PS > BOJ' 카테고리의 다른 글

[백준] 랜선자르기  (0) 2020.09.17
[백준] 음악프로그램  (0) 2020.09.17
[백준] 나무 자르기  (0) 2020.09.17
[백준] 유기농 배추  (0) 2020.09.17
[백준] 기타 레슨  (0) 2020.09.17
Comments