[9095] 1,2,3 더하기

Worldi·2021년 2월 12일
0

알고리즘

목록 보기
6/59

재귀 방법을 이용해 풀어보았다

-void
void hello(int target ,int sum) {
if (sum == target) {
count1++;
}
if (sum > target) return;
hello(target, sum + 1);
hello(target, sum + 2);
hello(target, sum + 3);
}

  • int
    int hello1(int target, int sum) {
    if (sum == target) return 1;
    if (sum > target) return 0;
    int count = 0;
    for (int i = 1; i <= 3; i++) {
    count += hello1(target, sum + i);
    }
    return count;
    }
profile
https://worldi.tistory.com/ 로 블로그 이전합니다.

0개의 댓글