[swexpert] 1989. 초심자의 회문 검사 (python, java)

728x90
반응형

문자열 끝에 엔터도 입력되므로 제거해준 후 뒤집어서 같은지 비교해진다 

 

1. 파이썬

t=int(input())

for i in range(1,t+1):
    s=input().strip()
    if s==s[::-1]:
        print(f'#{i} 1')
    else:
        print(f'#{i} 0')

 

2. 자바 

 

new StringBuffer로 뒤집을 수 있는데 다시 toString()으로 해줘야 같은지 아닌지 비교가 되는 문자열로 변환된다.

import java.util.Scanner;

public class Solution {
	
	
public static void main(String[] args) {
		
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		sc.nextLine();
		for (int tc = 1; tc <=t; tc++) {		
			String s=sc.next();
			System.out.printf("#%d ",tc);
			if(new StringBuffer(s).reverse().toString().equals(s))System.out.printf("%d",1);
			else {
				System.out.printf("%d",0);
			}
			System.out.println();
		}
		sc.close();
	}
	
	
}
728x90
반응형
TAGS.

Comments