[프로그래머스] 타겟 넘버(C++)

2025. 4. 19. 18:16·개발/알고리즘

문제 링크

https://school.programmers.co.kr/learn/courses/30/lessons/43165

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

코드 구현

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int answer = 0;

void dfs(const vector<int>& numbers, int _Index, int _sum, const int& _target)
{
    if (_Index == numbers.size())
    {
        if (_sum == _target) answer++;
        return;
    }
      
    dfs(numbers, _Index + 1, _sum + numbers[_Index], _target);
    dfs(numbers, _Index + 1, _sum - numbers[_Index], _target);
}

int solution(vector<int> numbers, int target) 
{
    dfs(numbers, 0, 0, target);

    return answer;
}

 

 

해결 방법

모든 경우의 수를 순회하도록 재귀 함수를 이용한 DFS 알고리즘을 사용하였다. 

'개발 > 알고리즘' 카테고리의 다른 글

[백준] 스타트와 링크(C++)  (2) 2025.04.25
[프로그래머스] 하노이의 탑(C++)  (1) 2025.04.22
[백준] 2606번: 바이러스(C++)  (2) 2025.04.18
[프로그래머스] 미로 탈출(C++)  (1) 2025.04.17
[프로그래머스] 게임 맵 최단거리(C++)  (1) 2025.04.15
'개발/알고리즘' 카테고리의 다른 글
  • [백준] 스타트와 링크(C++)
  • [프로그래머스] 하노이의 탑(C++)
  • [백준] 2606번: 바이러스(C++)
  • [프로그래머스] 미로 탈출(C++)
Majangnan
Majangnan
  • Majangnan
    개발 모코코
    Majangnan
  • 전체
    오늘
    어제
    • 분류 전체보기 (60) N
      • 개발 (59) N
        • C# (10)
        • SQL (3)
        • Unity (8)
        • Unreal (10)
        • C++ (2)
        • Server (1)
        • DX11 (8)
        • 알고리즘 (16) N
  • 블로그 메뉴

    • 홈
    • 방명록
    • 깃허브
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    sql
    슈팅게임
    UnReal
    DX11
    dx3d
    상속
    알고리즘
    blueprint
    Mecanim
    c++
    코딩테스트
    블루프린트
    DirectX11
    C#
    백준
    Unity
    3dlight
    프로그래머스
    MAC
    언리얼
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Majangnan
[프로그래머스] 타겟 넘버(C++)
상단으로

티스토리툴바