[프로그래머스] 타겟넘버
#include <string>
#include <vector>
using namespace std;
// global variable
int answer = 0;
void get_target_number(vector<int> numbers, int target, int sum, int index) {
// end condition
if (index == numbers.size()) {
if (sum == target)
answer++;
// if it reached end : return
return;
}
// recursive code is divided with to brunch
// first : + number[index]
// second : - number[index]
// elements of list have two cases on one
// therefore this recursive code can find all cases
get_target_number(numbers, target, sum + numbers[index], index + 1);
get_target_number(numbers, target, sum - numbers[index], index + 1);
}
int solution(vector<int> numbers, int target) {
get_target_number(numbers, target, 0, 0);
return answer;
}
첫날 풀어서 풀이를 까먹었다
주석의 중요성에 대해 다시 생각하고 갑니다..