[C++] 수식 만들기 - DFS

2026. 9. 28. 22:58·coding test - C++/기본기문제
728x90
반응형

// 85 수식 만들기 

#include <vector>
#include <string>
#include <vector>
#include <iostream>
using namespace std;

int n = 3;
vector<int> arr = {5, 3, 8};
vector<int> cal = {1,0,1,0}; // 0: +, 1: -, 2: x, 3: /
// 연산을 했을때 최댓값과 최솟값 리턴하기

int max_ = -10000000;
int min_ = 10000000;

void DFS(int L, int total, vector<int> ch){
    if (L == n - 1){
        if (max_ < total){
            max_ = total;
        }
        if (min_ > total){
            min_ = total;
        }
        return;
    }
    for(int i = 0; i < 4; i++){
        if (ch[i] > 0){
            ch[i] -= 1;
            if (i == 0){
                DFS(L+1, total + arr[L+1],ch);
            }
            else if(i == 1){
                DFS(L+1, total - arr[L+1],ch);
            }
            else if(i == 2){
                DFS(L+1, total * arr[L+1],ch);
            }
            else if(i == 3){
                DFS(L+1, total / arr[L+1],ch);
            }
            ch[i] += 1;
        }
    }
}
int main(){

    DFS(0,arr[0],cal);
    cout << max_ << endl;
    cout << min_ << endl;


    return 0;
}
728x90
반응형

'coding test - C++ > 기본기문제' 카테고리의 다른 글

[C++] 미로탐색 - DFS 경로 수 구하기  (0) 2026.09.29
[C++] 경로 탐색 (DFS)  (0) 2026.09.28
[C++] 기본 문법 (매크로, 구조체, 연산)  (0) 2026.02.10
[C++] 주요 알고리즘 정리 (BFS, DFS, 다익스트라...)  (1) 2026.02.07
[C++] 코딩테스트를 위한 문법 & 알고리즘 정리  (0) 2026.02.07
'coding test - C++/기본기문제' 카테고리의 다른 글
  • [C++] 미로탐색 - DFS 경로 수 구하기
  • [C++] 경로 탐색 (DFS)
  • [C++] 기본 문법 (매크로, 구조체, 연산)
  • [C++] 주요 알고리즘 정리 (BFS, DFS, 다익스트라...)
sillon
sillon
꾸준해지려고 합니다..
    반응형
  • sillon
    sillon coding
    sillon
  • 전체
    오늘
    어제
    • menu (657) N
      • notice (2)
      • python (68)
        • 자료구조 & 알고리즘 (23)
        • 라이브러리 (19)
        • 기초 (8)
        • 자동화 (14)
        • 보안 (1)
      • BIO (0)
        • Basic (2)
      • coding test - python (305)
        • Programmers (170)
        • 백준 (76)
        • Code Tree (22)
        • 기본기 문제 (37)
      • coding test - C++ (3)
        • Programmers (14)
        • 백준 (8)
        • 기본기문제 (14)
      • 공부정리 (6) N
        • 신호처리 시스템 (0)
        • Deep learnig & Machine lear.. (41)
        • Data Science (18)
        • Computer Vision (17)
        • NLP (40)
        • Dacon (2)
        • 모두를 위한 딥러닝 (강의 정리) (4)
        • 모두의 딥러닝 (교재 정리) (9)
        • 통계 (3)
      • HCI (23)
        • Haptics (7)
        • Graphics (11)
        • Arduino (4)
      • Project (21)
        • Web Project (1)
        • App Project (1)
        • Paper Project (1)
        • 캡스톤디자인2 (17)
        • etc (1)
      • OS (10)
        • Ubuntu (9)
        • Rasberry pi (1)
      • App & Web (9)
        • Android (7)
        • javascript (2)
      • C++ (5)
        • 기초 (5)
      • Cloud & SERVER (8)
        • Git (2)
        • Docker (1)
        • DB (4)
      • Paper (7)
        • NLP Paper review (6)
      • 데이터 분석 (1)
        • GIS (0)
      • daily (2)
        • 대학원 준비 (0)
      • 영어공부 (8)
        • job interview (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    Python
    programmers
    백준
    소수
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
sillon
[C++] 수식 만들기 - DFS
상단으로

티스토리툴바