스파르탄 365 2주차 (3) - 모든 경우의 수

류지연·2021년 5월 6일
0
post-thumbnail

1. 문제 링크

https://online.spartacodingclub.kr/enrolleds/606a6494ca5c2e219f9ca31e/ehomeworks/606a6494ca5c2e219f9ca352

2. 풀이 전 생각

  • 1 다섯 개로 3 만들기
  • 마이너스와 플러스를 이용한 식 성립
  • 탈출조건을 만들어주어야 한다.

3. 풀이

numbers = [2, 3, 1]
target_number = 0
result_count = 0


def get_count_of_ways_to_target_by_doing_plus_or_minus(array, target, current_index, current_sum):
    if current_index == len(array):  
        if current_sum == target:
            global result_count
            result_count += 1 
        return
    get_count_of_ways_to_target_by_doing_plus_or_minus(array, target, current_index + 1,
                                                       current_sum + array[current_index])
    get_count_of_ways_to_target_by_doing_plus_or_minus(array, target, current_index + 1,
                                                       current_sum - array[current_index])


get_count_of_ways_to_target_by_doing_plus_or_minus(numbers, target_number, 0, 0)
print(result_count)  

4. 소감

아직까지는 코딩에 있어서 부족한 점이 많은 것 같다. 진도에 맞춰 하나하나씩 차근히 배워나가되 스스로 채울 수 있는 부분을 찾아 만들어내야겠다.

profile
개발자

0개의 댓글