SWEA3499 퍼펙트 셔플

Queue를 활용해 가장 앞쪽의 값을 뽑아 새로운 List에 담아주면 된다.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class Solution {
	static BufferedReader br;
	static BufferedWriter bw;
	static StringTokenizer st;
	static LinkedList<String> p1, p2, output;

	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		bw = new BufferedWriter(new OutputStreamWriter(System.out));
		int T = Integer.parseInt(br.readLine());
		
		for (int tc = 1; tc <= T; tc++) {
			int N = Integer.parseInt(br.readLine());
			p1 = new LinkedList<>();
			p2 = new LinkedList<>();
			output = new LinkedList<>();
			st = new StringTokenizer(br.readLine(), " ");
			for (int i = 0; i < (N + 1) / 2; i++) {
				p1.add(st.nextToken());
			}
			for (int i = (N+1) / 2; i < N; i++) {
				p2.add(st.nextToken());
			}
			for(int i = 0; i < N; i++) {
				if(i % 2 == 0) {
					output.add(p1.poll());
				}else {
					output.add(p2.poll());
				}
			}
			
			bw.append("#" + tc + " ");
			for(String t : output) {
				bw.append(t + " ");
			}
			bw.append("\n");

		}
		bw.flush();
		bw.close();

	}
}

좋은 웹페이지 즐겨찾기