Notice
Recent Posts
Recent Comments
Link
«   2026/05   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Archives
Today
Total
관리 메뉴

mo1lusca의 블로그

[백준] 25501 재귀의 귀재 - C 본문

PS

[백준] 25501 재귀의 귀재 - C

mo1lusca 2025. 5. 1. 08:39

 

 

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

 

 


 

isPalindrome 함수의 반환값과 recursion 함수의 호출횟수를 출력하면 되는 문제이다.

 

#include <stdio.h>
#include <string.h>

int cnt = 0;

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() {


    int n;
    scanf("%d", &n);

    for (int i = 0; i < n; i++) {
        cnt = 0;
        char s[1000];
        scanf("%s", s);

        int temp = isPalindrome(s);

        printf("%d %d\n", temp, cnt);

    }


}

 

 

전역변수 cnt를 사용하여 recursion 함수의 호출 횟수를 세었다.

printf 함수에서 바로 isPalindrome을 호출하면 cnt가 isPalindrome보다 나중에 평가된다는 보장이 없으므로

printf 함수 전에 isPalindrome의 값을 temp에 넣어놓았다.