Adventure Time - Finn 3
본문 바로가기
코테연습/python

수들의 합

by hyun9_9 2026. 4. 13.
import sys
sys.stdin=open("input.txt","rt")
n,m = map(int,input().split())
# n = int(input())
arr = list(map(int,input().split()))
# n = input()

# cnt = 0
# 하나하나 전부 구하게 됨
# for i in range(n):
#     sum = 0
#     for j in range(i,n):
#         sum += arr[j]
#         if sum == m:
#             cnt +=1
#             break
#         if sum > m:
#             break
cnt = 0
tot = 0
rt = 0
lt = 0
while True:
    if tot < m:
        if rt <n:
            tot += arr[rt]
            rt +=1
        else:
            break
    elif tot == m:
        cnt+=1
        tot -= arr[lt]
        lt +=1
    else:
        tot -= arr[lt]
        lt+=1


print(cnt)

N개의 수로 된 수열 A[1], A[2], …, A[N] 이 있다. 이 수열의 i번째 수부터 j번째 수까지의 합 A[i]+A[i+1]+…+A[j-1]+A[j]가 M이 되는 경우의 수를 구하는 프로그램을 작성하시오.

 

'코테연습 > python' 카테고리의 다른 글

곳감(모래시계)  (1) 2026.04.14
격자판 최대합  (0) 2026.04.13
두 리스트 합치기  (0) 2026.04.13
카드 역배치(정올 기출)  (0) 2026.04.12
숫자만 추출  (0) 2026.04.12