백준

백준 25501 - 재귀의 귀재(C++)

공대생의 잡다한 사전 2023. 6. 18. 20:58

문제 링크입니다. https://www.acmicpc.net/problem/25501

 

25501번: 재귀의 귀재

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

www.acmicpc.net

문제에서 주어진 함수를 사용한 뒤, 호출 개수를 세는 것만 추가해주면 됩니다.

 

 

자세한 것은 코드를 참고해주세요.

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <map>

using namespace std;

int N;
int cnt;

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

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


int main(){
    cin.tie(0);
    cout.tie(0);
    cin >> N;
    for(int i = 1; i <= N; i++){
        char x[1001];
        cnt = 0;
        scanf("%s", x);
        printf("%d ",isPalindrome(x));
        printf("%d\n", cnt);
    }
    return 0;
}

 

 

질문 및 조언은 댓글을 남겨주세요