프로그래밍 /코딩 연습

구글 코딩 잼 연습 - Reverse Words 솔류션

Aerosky 2014. 1. 22. 04:45

[출처: https://code.google.com/codejam/contest/351101/dashboard#s=p1]



연습문제라서 포스팅 해도 괜찮겠죠 ㅎㅎ 사용된 프로그래밍 언어는 Java 입니다 ^^


Problem

Given a list of space separated words, reverse the order of the words. Each line of text contains L letters and W words. A line will only consist of letters and space characters. There will be exactly one space character between each pair of consecutive words.


Input

The first line of input gives the number of cases, N.
N test cases follow. For each test case there will a line of letters and space characters indicating a list of space separated words. Spaces will not appear at the start or end of a line.


Output

For each test case, output one line containing "Case #x: " followed by the list of words in reverse order.

Limits

Small dataset

N = 5
1 ≤ L ≤ 25

Large dataset

N = 100
1 ≤ L ≤ 1000


Sample


Input 

Output 
3
this is a test
foobar
all your base
Case #1: test a is this
Case #2: foobar
Case #3: base your all


쉬운 문제에요 ^^ 파일을 주어진 케이스 숫자만큼 읽고 각 줄 하나가 케이스 하나를 나타내는데 여기에 주어진 String 가격을 거꾸로 바꿔서 출력하면 되는거죠. 우선 줄 하나가 문장이고 문장은 단어 하나하나마다 스페이스로 나눠져 있기 때문에 String 의 split() 함수를 사용해서 Array of String 에 저장시키고 출력 시킬때는 index 가격을 반대로 해서 출력시키면 되는거죠.



package com.google.practice;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ReverseWords {
	private final static String INPUT = "B-large-practice.in";
	private final static String OUTPUT = "B-large-practice.out";
	
	public static void main(String[] args) 
                throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new FileReader(new File(INPUT)));
		int numLine = Integer.parseInt(br.readLine());
		BufferedWriter bw = new BufferedWriter(new FileWriter(OUTPUT));
		for(int i=0; i < numLine; ++ i) {
			String[] strOriginal = br.readLine().split(" ");
			bw.write("Case #" + (i+1) + ": ");
			for(int j=(strOriginal.length-1); j >= 0; -- j) {
				bw.write(strOriginal[j]);
				bw.write(" ");
			}
			bw.write("\n");
		}
		bw.close();
		br.close();
	}
	
}