백준 12605번 단어순서 뒤집기-JAVA

sujin·2025년 1월 31일

코딩테스트-백준

목록 보기
2/18

📝문제

📝알고리즘
//전체 케이스 개수인 N을 입력받음
//각 케이스마다
//단어들을 입력받아 스페이스를 기준으로 나눠서 words에 저장함
//입력된 단어 순서대로 스택에 넣음
//스택이 빌 때까지 pop함

📝구현

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        Stack<String> stack=new Stack<>();
        int N=scanner.nextInt();
        scanner.nextLine();
        for(int i=0;i<N;i++){
            String text=scanner.nextLine();
            String[] words=text.split(" ");
            
            for(String word: words){
                stack.push(word);
            }
            System.out.print("Case #"+(i+1)+": ");
            while(!stack.isEmpty()){
                System.out.print(stack.pop()+" ");
            }
            System.out.println();
        }
    }
}
profile
열공!

0개의 댓글