Deff_Dev

[코드업] (재귀 함수) 1부터 n까지 출력하기 (C++) 본문

코딩테스트/코드업

[코드업] (재귀 함수) 1부터 n까지 출력하기 (C++)

Deff_a 2024. 3. 28. 13:53
 

(재귀 함수) 1부터 n까지 출력하기

$1$부터 정수 $n$까지 출력하는 재귀함수를 설계하시오. 이 문제는 반복문 for, while 등을 이용하여 풀수 없습니다. 금지 키워드 : for while goto

codeup.kr

 

문제

이 문제는 재귀 함수를 이용하여 1~n까지 출력하는 문제이다.

 

 

풀이

재귀 함수를 사용하여 1~n까지 출력했다.

 

#include <iostream>
using namespace std;
// https://codeup.kr/problem.php?id=1901&rid=0
int n;

void PrintFunc(int nowNum) { // 재귀 함수
    if (nowNum > n) {
        return;
    }
    cout << nowNum << endl;
    PrintFunc(nowNum + 1);
}
int main() {
    
    cin >> n;
    
    PrintFunc(1);

    return 0;
}

 

 

CodingTestPractice/CodeUp/재귀 함수/(재귀 함수) 1부터 n까지 출력하기.cpp at main · seungdo1234/CodingTestPracti

코딩 테스트 연습. Contribute to seungdo1234/CodingTestPractice development by creating an account on GitHub.

github.com

 

이해가 안되는 부분이나 개선해야될 부분이 있다면 댓글 달아주시면 감사드리겠습니다.