백준/브론즈

[백준] 브론즈2-재귀의 귀재 C++ 25501번

taene_ 2023. 9. 21. 19:40

https://www.acmicpc.net/problem/25501

 

25501번: 재귀의 귀재

각 테스트케이스마다, isPalindrome 함수의 반환값과 recursion 함수의 호출 횟수를 한 줄에 공백으로 구분하여 출력한다.

www.acmicpc.net

#include <string>
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;

int countP;

int recursion(const char* s, int len, int r) 
{
    countP++;
    if (len >= r) return 1;
    else if (s[len] != s[r]) return 0;
    else return recursion(s, len + 1, r - 1);
}

int isPalindrome(const char* s) 
{
    return recursion(s, 0, strlen(s) - 1);
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        char s[1001];
        cin >> s;
        cout << isPalindrome(s) << ' ' << countP << '\n';
        countP = 0;
    }

    return 0;
}

접근방법: 재귀함수를 이해하고 사용하자