和为k的子数组个数
查看提交统计提问总时间限制: 1000ms 内存限制: 65536kB
描述
给定一组整数数字和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数。
输入第一行:由空格区分的一组数字第二行:整数k
输出一个整数,代表多少子数组等于k
样例输入
1 1 1
2
样例输出
2
#include<bits/stdc++.h>
using namespace std;
int a[1000];
int main(){
int n=0,sum=0,s=0,k;
do{
n++;
cin>>a[n];
}while((getchar())!='\n');
cin>>k;
for(int x=1;x<=n;x++){
for(int i=1;i<=n-x+1;i++){
for(int j=i;j<i+x;j++){
sum+=a[j];
}
if(sum==k)s++;
sum=0;
}
}
cout<<s;
}