[백준#C++] 연산자 끼워넣기
문제
https://www.acmicpc.net/problem/15658
접근 과정
- 조건에 맞게 각각 DFS
코드
#include <iostream>
#include <algorithm>
using namespace std;
 
int n;
int a[13];
int cal[4];
long long min_ans = 1000000000;
long long max_ans = -1000000000;
 
void input(){
    cin>>n;
    for(int i=0; i<n; i++){
        cin>>a[i];
    }
    for(int i=0; i<4; i++){   
       cin>>cal[i];
    }
}
 
void dfs(int depth, int plus, int minus, int multi, int div, int res){
	if( depth == n){
		if (max_ans < res) max_ans = res; 
		if (min_ans > res) min_ans = res; 
		return;
	}
 
	if (plus > 0) dfs( depth + 1, plus - 1, minus, multi, div, res + a[depth]);
	if (minus > 0) dfs( depth + 1, plus, minus - 1, multi, div, res - a[depth]);
	if (multi > 0) dfs( depth + 1, plus, minus, multi - 1, div, res * a[depth]);
	if (div > 0) dfs( depth + 1, plus, minus, multi, div - 1, res / a[depth]);
}
 
int main() {
	input();
 
	dfs(1 , cal[0], cal[1], cal[2], cal[3], a[0]);
 
    cout<<max_ans<<"\n";
    cout<<min_ans<<"\n";
	return 0;
}Author And Source
이 문제에 관하여([백준#C++] 연산자 끼워넣기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@tunakim/백준C-두-동전저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)