给定一个数组a[0],a[1],a[2],a[3]....a[n]。请问中间加和等于k的连续子串有多少个?
第一行输入两个整数n,k。n代表数组长度,k代表所求子串的和。0 < k < 10000000, 0 < n <= 100000;
第二行为n个整数,0 <=a[i] <= 100000;
输出一个整数代表等于k的连续子串的个数。
5 4 1 2 3 1 2
1
def solu():
try:
n,k = input().split(' ')
except:
return False
n,k = int(n),int(k)
nums = input().split(' ')
nums = [int(_) for _ in nums]
r,l,c,res = 0,0,0,0
while l<n:
while c<k and l<n:
c +=nums[l]
l+=1
while c==k and l<n:
res +=1
c +=nums[l]
l +=1
while c>k and r<l:
c -= nums[r]
r +=1
while c==k and r<l:
res +=1
c -=nums[r]
r +=1
print(res)
return True
if __name__=="__main__":
while solu():
pass