0. [c++] sw 7812


https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWslJ57qFS0DFASy&categoryId=AWslJ57qFS0DFASy&categoryType=CODE


 1. 풀이





 2. 소스코드


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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>
#include<cstring>
#include<algorithm>
 
using namespace std;
 
int arr[10001];
 
int makePortfolio(const int& N, const int& M) {
    int count = 0;
    pair<intint> idx;
    idx = { 0,0 };
    int sum = 0;
    for (int i = 0; i < N; i++) {
        if (sum < M) {
            sum += arr[i];
            idx.second = i;
        }
        if (sum == M) {
            count++;
            sum -= arr[idx.first];
            idx.first++;
        }
        if (sum > M)
        {
            while (1)
            {
                if (sum <= M)
                    break;
                sum -= arr[idx.first];
                idx.first++;
            }
            
        }
        if (sum == M) {
            count++;
            sum -= arr[idx.first];
            idx.first++;
        }
    }
    return count;
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int T, N, M;
    cin >> T;
    for (int i = 1; i <= T; i++) {
        cin >> N >> M;
        for (int j = 0; j < N; j++) {
            cin >> arr[j];
        }
        cout << "#" << i << " " << makePortfolio(N, M) << "\n";
    }
    return 0;
}
cs


 3. 참고




질문이나 지적 있으시면 댓글로 남겨주세요~

도움 되셨으면 하트 꾹!


+ Recent posts