Data Structures & Algorithms

[Algorithms] Basic-수들의 합

숄구-ml 2022. 5. 14. 12:00

  • 포인터를 이용하는 문제
  • 부분집합의 합이 M이 되는 경우의 수 구하는 문제
  • 원소 1개 = M일 때도 인정 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
n, m=map(int, input().split())
a=list(map(int, input().split()))
lt, rt = 01
tot=a[0]
cnt=0
 
while True:
    if tot < m:
        if rt < n:
            tot+=a[rt]
            rt+=1
        else:       #rt가 n에 도달하면 while문 탈출
            break
    elif tot == m:
        cnt+=1
        tot-=a[lt]
        lt+=1
    else:
        tot-=a[lt]
        lt+=1
print(cnt)
cs

 

 

728x90