๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 15658๋ฒˆ - ์—ฐ์‚ฐ์ž ๋ผ์›Œ๋„ฃ๊ธฐ (2)

by dkswnkk 2022. 8. 23.

๋ฌธ์ œ

 

15658๋ฒˆ: ์—ฐ์‚ฐ์ž ๋ผ์›Œ๋„ฃ๊ธฐ (2)

์ฒซ์งธ ์ค„์— ์ˆ˜์˜ ๊ฐœ์ˆ˜ N(2 ≤ N ≤ 11)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” A1, A2, ..., AN์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ Ai ≤ 100) ์…‹์งธ ์ค„์—๋Š” ํ•ฉ์ด N-1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 4N๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ 4๊ฐœ์˜ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋Š”๋ฐ, ์ฐจ๋ก€๋Œ€

www.acmicpc.net

 

์ฝ”๋“œ

#include <iostream>
#include <vector>

using namespace std;

int N;
int _add, _sub, _mul, _div;
vector<int> v;

int _max = -1e9, _min = 1e9;

void dfs(int add, int sub, int mul, int div, int result, int idx){
    if(idx == N){
        _max = max(_max, result);
        _min = min(_min, result);
        return;
    }
    
    if(add>0) dfs(add-1, sub, mul, div, result + v[idx], idx +1);
    if(sub>0) dfs(add, sub-1, mul, div, result - v[idx], idx +1);
    if(mul>0) dfs(add, sub, mul-1, div, result * v[idx], idx +1);
    if(div>0) dfs(add, sub, mul, div-1, result / v[idx], idx +1);
    
    
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    cin>>N;
    for(int i=0; i<N; i++){
        int inp; cin>>inp;
        v.push_back(inp);
    }
    
    cin>>_add>>_sub>>_mul>>_div;
    dfs(_add, _sub, _mul, _div, v[0], 1);
    cout<<_max << ' '<< _min;
    
    
}

 

ํ’€์ด

 

[๋ฐฑ์ค€,c++] 14888๋ฒˆ - ์—ฐ์‚ฐ์ž ๋ผ์›Œ๋„ฃ๊ธฐ

๋ฌธ์ œ 14888๋ฒˆ: ์—ฐ์‚ฐ์ž ๋ผ์›Œ๋„ฃ๊ธฐ ์ฒซ์งธ ์ค„์— ์ˆ˜์˜ ๊ฐœ์ˆ˜ N(2 ≤ N ≤ 11)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” A1, A2, ..., AN์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ Ai ≤ 100) ์…‹์งธ ์ค„์—๋Š” ํ•ฉ์ด N-1์ธ 4๊ฐœ์˜ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋Š”๋ฐ, ์ฐจ๋ก€๋Œ€๋กœ ๋ง

dkswnkk.tistory.com

์œ„์™€ ๋™์ผํ•œ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

๋Œ“๊ธ€