Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 그리디알고리즘 #그리디 #백준 #우선순위큐 #최소힙 #최대힙 #알고리즘 #코딩테스트 #python
- 프로그래머스 #네트워크 #c++ #코딩테스트 #코테 #코테준비 #dfs
- 백준 #백준알고리즘 #알고리즘 #코딩테스트 #코딩테스트준비 #코테준비 #백준2110 #python #문제풀이
- 동
- 프로그래머스 #python #2021카카오 #카카오코테 #카카오인턴쉽
- 카카오 코테
- 프로그래머스 #sql #mysql #코딩테스트
- 백준 #이거다시풀기
- 백준 #백준2217 #백준로프 #python
- 프로그래머스 #c++ #코딩테스트
- 프로그래머스 #python #코딩테스트 #코테공부 #알고리즘 #dict
- 카카오 #프로그래머스 #python #코딩테스트 #오픈채팅방
- 프로그래머스 #NULL 처리하기
Archives
- Today
- Total
say repository
[프로그래머스] 타겟 넘버 c++ 본문
728x90
출처: 프로그래머스
https://programmers.co.kr/learn/courses/30/lessons/43165?language=cpp
코딩테스트 연습 - 타겟 넘버
n개의 음이 아닌 정수들이 있습니다. 이 정수들을 순서를 바꾸지 않고 적절히 더하거나 빼서 타겟 넘버를 만들려고 합니다. 예를 들어 [1, 1, 1, 1, 1]로 숫자 3을 만들려면 다음 다섯 방법을 쓸 수
programmers.co.kr
#include <string>
#include <vector>
using namespace std;
int answer = 0;
void dfs(vector<int> numbers, int target, int total, int index){
if (index == numbers.size()){
if (total == target){
answer ++ ;
}
return;
}
dfs(numbers, target, total + numbers[index], index + 1);
dfs(numbers, target, total - numbers[index], index + 1);
}
int solution(vector<int> numbers, int target) {
dfs(numbers, target, numbers[0], 1);
dfs(numbers, target, -numbers[0], 1);
return answer;
}
'알고리즘 문제 풀이 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 단어 변환 c++ (*) (0) | 2022.03.19 |
---|---|
[프로그래머스] 네트워크 c++ (0) | 2022.03.18 |
[프로그래머스] 카펫 c++ (0) | 2022.03.17 |
[프로그래머스] 소수 찾기 c++ (0) | 2022.03.17 |
[프로그래머스] 모의고사 c++ (0) | 2022.03.17 |