본문 바로가기

코딩테스트/C++

백준 브루트 포스 알고리즘 2798번 C++

단계별로 풀어보기 12_1_2798

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main() {

    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    
	int N, M;
	cin >> N >> M;
	int arr[101];
	int num;
	for (int i = 0; i < N; i++) {
		cin >> num;
		arr[i] = num;
	}

	int index = 0;
	int min=300000;
	
	for (int i = 0; i < N; i++) {
		for (int j = i + 1; j < N; j++) {
			for (int k = j + 1; k < N; k++) {
				index = M - (arr[i] + arr[j] + arr[k]);
				if (min > index && index >= 0) {
					min = index;
				}
			}
		}
	}
	cout << M - min;
}

for문을 3번이나 겹쳐서 시간복잡도가 최악이겠지만 현재 상태에선 이런 코드가 최선인 것 같다..

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

이 코드를 사용해 최대한 입출력시간을 줄여주려는 노력이라도 했다!!

 

 

 

 

// 백준 알고리즘, 백준 알고리즘 기초, 백준 알고리즘 종류,코딩 테스트 백준.알고리즘