본문 바로가기

Algorithm/백준

백준/ 바킹독님의 문제집/ 재귀함수가 뭔가요?(17478)


문제 설명


문제 해결

띄어쓰기 한 글자라도 틀리면 아예 틀렸다고 판정할만큼 까다롭다.

함수의 정의는 void func(int count)으로 해줬다.

재귀 함수의 base condition은 count == N으로 설정했다.

재귀식은 func(count + 1)로 설정하여 count를 차근차근 증가시키면된다.

mask라는 함수를 만들어줬는데 count가 증가함에 따라서 "____"를 더 출력해주는 함수다.

#include <iostream>
#include <queue>
#include <utility>
#include <tuple>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <algorithm>

using namespace std;

int N;

void mask(int count)
{
	for (int i = 0; i < count; i++)
	{
		cout << "____";
	}
}

void func(int count)
{
	if (count == N)
	{
		mask(count);
		cout << "\"재귀함수가 뭔가요?\"" << "\n";
		mask(count);
		cout << "\"재귀함수는 자기 자신을 호출하는 함수라네\"" << "\n";
		mask(count);
		cout << "라고 답변하였지." << "\n";
		return ;
	}
	mask(count);
	cout << "\"재귀함수가 뭔가요?\"" << "\n";
	mask(count);
	cout << "\"잘 들어보게. 옛날옛날 한 산 꼭대기에 이세상 모든 지식을 통달한 선인이 있었어." << "\n";
	mask(count);
	cout << "마을 사람들은 모두 그 선인에게 수많은 질문을 했고, 모두 지혜롭게 대답해 주었지." << "\n";
	mask(count);
	cout << "그의 답은 대부분 옳았다고 하네. 그런데 어느 날, 그 선인에게 한 선비가 찾아와서 물었어.\"" << "\n";
	
	func(count+1);

	mask(count);
	cout << "라고 답변하였지." << "\n";
}

int main(int argc, char* argv[])
{
	ios::sync_with_stdio(false);
	cout.tie(nullptr);
	cin.tie(nullptr);
	cin >> N;

	cout << "어느 한 컴퓨터공학과 학생이 유명한 교수님을 찾아가 물었다." << '\n';
	func(0);

	return 0;
}

프로그램이 제대로 문자열을 출력했는지 확인하려면 아래 사이트를 이용해서

예제와 프로그램이 출력한 결과를 비교하면 될 것 같다.

텍스트 비교
https://kr.piliapp.com/text-diff/#diff