Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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
Archives
Today
Total
관리 메뉴

킹솔이

[프로그래머스/C++] 서머코딩/윈터코딩(~2018) 쿠키 구입 본문

Algorithm

[프로그래머스/C++] 서머코딩/윈터코딩(~2018) 쿠키 구입

킹솔이 2020. 3. 9. 19:35
#include <string>
#include <vector>
#include<iostream>

using namespace std;

int solution(vector<int> cookie) {
	int answer = 0;
	int n = cookie.size();
	vector<int> sum(n);
	int son1;
	int son2;
	int temp;

	sum[0] = cookie[0];
	for (int i = 1; i < n; i++) 
		sum[i] = sum[i - 1] + cookie[i];

	sum.insert(sum.begin(), 0);

	for (int r = 2; r < n+1; r++) {
		for (int m = 1; m < r; m++) {
			son1 = sum[r] - sum[m];
			if (son1 < answer) continue;
			for (int l = 1; l <= m; l++) {
				son2 = sum[m] - sum[l-1];

				if (son1 == son2) {
					temp = son1;
					if (temp > answer) answer = temp;
				}
				else if (son1 > son2) break;
			}
		}
	}

	
	return answer;
}

int main() {
	vector<int> cookie = { 1,1, 1,1, 1,1, 1,1};
	cout << solution(cookie);
	return 0;
}