문제
https://www.acmicpc.net/problem/25501
25501번: 재귀의 귀재
각 테스트케이스마다, isPalindrome 함수의 반환값과 recursion 함수의 호출 횟수를 한 줄에 공백으로 구분하여 출력한다.
www.acmicpc.net
내가 작성한 코드
import java.io.*;
public class qor25501 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int number = Integer.parseInt(br.readLine());
for(int i = 0; i<number; i++){
String testcase = br.readLine();
int count = 0;
bw.write(isPalindrome(testcase, count));
bw.newLine();
}
bw.close();
}
public static String recursion(String s, int l, int r, int count){
count++;
if(l >= r) return "1 "+count;
else if(s.charAt(l) != s.charAt(r)) return "0 "+count;
else return recursion(s, l+1, r-1, count);
}
public static String isPalindrome(String s, int count){
return recursion(s, 0, s.length()-1, count);
}
}
'자바 > 백준' 카테고리의 다른 글
[백준 1302] 베스트셀러 (0) | 2024.08.07 |
---|---|
[백준 17478] 재귀함수가 뭔가요? (0) | 2023.04.04 |
[백준 1547] 공 (0) | 2023.02.09 |
[백준 1284] 집 주소 (0) | 2023.02.09 |
[백준 2441] 별 찍기 - 4 (0) | 2023.02.02 |