| #include<iostream> using namespace std; | |
| int num[12]; | |
| int op[4]; // +, -, *, / | |
| int mn, mx; | |
| int N; | |
| void solve(int depth, int res); | |
| int main(int argc, char** argv) | |
| { | |
| int test_case; | |
| int T; | |
| cin>>T; | |
| for(test_case = 1; test_case <= T; ++test_case) | |
| { | |
| cin >> N; | |
| for(int i=0; i<4; i++) cin >> op[i]; | |
| for(int i=0; i<N; i++) cin >> num[i]; | |
| mn= 100000000 ; | |
| mx=-100000000; | |
| solve(0, num[0]); | |
| cout << "#" << test_case << " " << mx-mn <<endl; | |
| } | |
| return 0;//정상종료시 반드시 0을 리턴해야합니다. | |
| } | |
| void solve(int depth, int res){ | |
| if(depth==N-1){ | |
| if(res<mn) mn=res; | |
| if(res>mx) mx=res; | |
| |
| return; | |
| } | |
| for(int i=0; i<4; i++){ | |
| if(op[i]>0){ | |
| op[i]--; | |
| int tmp; | |
| if(i==0) tmp=res+num[depth+1]; | |
| if(i==1) tmp=res-num[depth+1]; | |
| if(i==2) tmp=res*num[depth+1]; | |
| if(i==3) tmp=res/num[depth+1]; | |
| solve(depth+1, tmp); | |
| op[i]++; | |
| } | |
| } | |
| } |
'알고리즘 문제 풀이 > 코딩 테스트 대비(~211220)' 카테고리의 다른 글
| 백준 1712 손익분기점 (0) | 2021.04.15 |
|---|---|
| 180413_점심식사시간 (0) | 2018.04.13 |
| 디저트 카페 (0) | 2018.04.09 |
| 활주로 건설 (0) | 2018.03.29 |
| 특이한 자석 (0) | 2018.03.28 |