IT/코딩테스트 연습

[백준] 9086 문자열

j8970 2025. 2. 22. 23:30

컴파일 에러를 확인해보니

세미콜론을 자꾸 까먹는다...

맞춘 코드!!

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int T = sc.nextInt();
        sc.nextLine();
        
        for (int i = 0; i < T; i++) {
            String str = sc.nextLine();
            char first = str.charAt(0);
            char last = str.charAt(str.length() - 1);
            
            System.out.println("" + first + last);
        }
        
        sc.close();
    }

 

값을 특정 한 문자로 불러와도

맞출 수 있는 문제라서

.charAt(인덱스 번호) 를 활용해서

문제를 해결했다!!

 

char 값으로 first 와 last에

할당 했으므로 출력할 때에

문자열로 변환 필수!

 

파이썬에선 더 쉽게 풀었던 것 같은데...

자바 쉽지 않네요...

 

알고리즘 공부할 겸 풀어본 파이썬 코드

T = int(input())

for _ in range(T):
    s = input()
    print(s[0] + s[-1])

 

파이썬 좋다...ㅠ

'IT > 코딩테스트 연습' 카테고리의 다른 글

[백준] 11718번 그대로 출력하기  (0) 2025.02.21