지우너
[프로그래머스] 타겟넘버 C++ 본문
문제
https://school.programmers.co.kr/learn/courses/30/lessons/43165
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
풀이
사실 dfs/bfs 유형이라고 적혀 있는데, 어떻게 풀어야 할지 감이 오지 않았다.
질문하기의 위 글을 보고 코드를 짤 수 있었다.
코드
#include <string>
#include <vector>
using namespace std;
void dfs(int idx, const vector<int> &numbers, int sum, int target, int& answer){
// 종료 조건
if(idx==numbers.size()){
if(sum==target){
answer++;
}
return;
}
// 재귀 호출
dfs(idx+1, numbers, sum+numbers[idx], target, answer);
dfs(idx+1, numbers, sum-numbers[idx], target, answer);
}
int solution(vector<int> numbers, int target) {
int answer = 0;
dfs(0, numbers, 0, target, answer);
return answer;
}
'Problem Solving' 카테고리의 다른 글
[프로그래머스] 게임 맵 최단거리 C++ (0) | 2025.03.12 |
---|---|
[프로그래머스] 전력망을 둘로 나누기 C++ (0) | 2025.03.10 |
[프로그래머스] 피로도 C++ (0) | 2025.03.09 |
[프로그래머스] k번째수 C++ (0) | 2025.02.21 |
[프로그래머스] 카펫 C++ (0) | 2025.02.21 |