Little Jay

[C++] 백준 17127번 - 벚꽃이 정보섬에 피어난 이유 본문

알고리즘/BOJ

[C++] 백준 17127번 - 벚꽃이 정보섬에 피어난 이유

Jay, Lee 2022. 5. 9. 18:51

파티션을 활용한 브루트포스 문제

#include <bits/stdc++.h>
#define endl '\n'
using namespace std;

int n;
int arr[11];

int calc(int start, int end) {
	int x = 1;
	for (int i = start; i <= end; i++) {
		x *= arr[i];
	}
	return x;
}

int main() {

	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> arr[i];
	}

	int temp, ans = 0;
	for (int i = 1; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			for (int k = j + 1; k < n; k++) {
				temp = calc(1, i) + calc(i + 1, j) + calc(j + 1, k) + calc(k + 1, n);
				ans = max(ans, temp);
			}
		}
	}

	cout << ans << endl;

	return 0;
}
Comments