코테/백준
[백준/JAVA] 25501번: 재귀의 귀재
imname1am
2023. 3. 27. 21:29
반응형
🔺 문제
25501번: 재귀의 귀재
각 테스트케이스마다, isPalindrome 함수의 반환값과 recursion 함수의 호출 횟수를 한 줄에 공백으로 구분하여 출력한다.
www.acmicpc.net
🔺 코드
import java.util.*;
import java.io.*;
public class Main {
static int cnt;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] arr = new String[n];
for(int i=0 ; i < n ; i++) {
cnt = 0;
arr[i] = br.readLine();
System.out.println(isPalindrome(arr[i]) + " " + cnt);
}
}
public static int recursion(String s, int l, int r) {
cnt++;
if(l >= r) return 1;
else if(s.charAt(l) != s.charAt(r)) return 0;
else return recursion(s, l+1, r-1);
}
public static int isPalindrome(String s) {
return recursion(s, 0, s.length() - 1);
}
}
✅ 해결 아이디어
- 팰린드롬이면 1, 아니면 0 리턴 (isPalindrome()
)
팰린드롬 함수.. 구현하라면 할 줄 알아야겠다!
반응형