본문 바로가기
JAVA/백준

[백준/자바] 12605번 단어순서 뒤집기

by 동백05 2022. 2. 1.

#1일1알고리즘

https://www.acmicpc.net/problem/12605

 

12605번: 단어순서 뒤집기

스페이스로 띄어쓰기 된 단어들의 리스트가 주어질때, 단어들을 반대 순서로 뒤집어라. 각 라인은 w개의 영단어로 이루어져 있으며, 총 L개의 알파벳을 가진다. 각 행은 알파벳과 스페이스로만

www.acmicpc.net

import java.util.Stack;
import java.util.Scanner;
public class feb01 {
	public static void main(String[] args) {
		/*Stack<Integer> stack=new Stack<>();
		stack.add(1); //스택에 값 넣기
		stack.capacity();
		stack.clear();//스택 비우기
		stack.contains(1);//괄호안의 값이 스택에 들어있는지 확인
		stack.empty();//스택이 비었는지 확인
		*/
		
		Scanner s=new Scanner(System.in);
		Stack<String> sstack=new Stack<>();
		int number=s.nextInt();
		s.nextLine();
		for(int i=0;i<number;i++) {
			String line=s.nextLine();
			String arr[]=line.split(" ");
			int size=arr.length;
			for(int j=0;j<size;j++) {
				sstack.add(arr[j]);
			}
			System.out.print("Case #"+(i+1)+": ");
			for(int j=0;j<size-1;j++) {
				System.out.print(sstack.pop()+" ");
			}
			System.out.println(sstack.pop());
		}
		
		
	}
}

https://www.acmicpc.net/problem/12605

댓글