-
Notifications
You must be signed in to change notification settings - Fork 5
/
p420.c
32 lines (31 loc) · 801 Bytes
/
p420.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* El secreto de la bolsa */
#include <string.h>
#include <stdio.h>
int main() {
int numCasos, sumaClave, res, suma, len;
char input[250001];
int i, j;
scanf("%d", &numCasos);
for(; numCasos > 0; numCasos--) {
scanf("%d %s", &sumaClave, input);
i = res = 0;
j = 0;
suma = input[0]-'0';
len = strlen(input);
while(i < len && j < len) {
if(suma < sumaClave) {
j++;
suma = suma + (input[j]-'0');
} else if(suma == sumaClave) {
res++;
suma = suma - (input[i]-'0');
i++;
} else {
suma = suma - (input[i]-'0');
i++;
}
}
printf("%d\n", res);
}
return 0;
}