Study
프로그래머스-타겟넘버 본문
코딩테스트 연습 - 타겟 넘버
n개의 음이 아닌 정수가 있습니다. 이 수를 적절히 더하거나 빼서 타겟 넘버를 만들려고 합니다. 예를 들어 [1, 1, 1, 1, 1]로 숫자 3을 만들려면 다음 다섯 방법을 쓸 수 있습니다. -1+1+1+1+1 = 3 +1-1+1+1+
programmers.co.kr
<풀이>
더보기
#include <string>
#include <vector>
using namespace std;
int answer = 0;
void targetNumber(vector<int> numbers, int target, int sum, int count) {
if(count == numbers.size()) {
if(sum == target) answer++;
return;
}
targetNumber(numbers, target, sum+numbers[count], count+1);
targetNumber(numbers, target, sum-numbers[count], count+1);
}
int solution(vector<int> numbers, int target) {
targetNumber(numbers, target, 0, 0);
return answer;
}
-참고한 사이트
'알고리즘 > C++ 문제풀이' 카테고리의 다른 글
프로그래머스-메뉴리뉴얼 (0) | 2021.07.28 |
---|---|
프로그래머스-오픈채팅방 (0) | 2021.07.22 |
프로그래머스-숫자의 표현 (0) | 2021.07.15 |
프로그래머스-크레인 인형뽑기 (0) | 2021.07.14 |
프로그래머스 -짝지어 제거하기 (0) | 2021.07.13 |