[D3] 퍼펙트 셔플 - 3499
성능 요약
메모리: 35,956 KB, 시간: 179 ms, 코드길이: 879 Bytes
제출 일자
2023-10-30 00:14
출처: SW Expert Academy, https://swexpertacademy.com/main/code/problem/problemList.do
import java.util.*;
class Solution
{
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int tc = 1; tc <= T; tc++)
{
int n = sc.nextInt();
Queue<String> queue1 = new LinkedList<>();
Queue<String> queue2 = new LinkedList<>();
for(int i = 0; i < (n/2 + n % 2); i++){
queue1.add(sc.next());
}
for(int i = 0; i < n/2; i++){
queue2.add(sc.next());
}
StringBuilder sb = new StringBuilder();
while(!queue2.isEmpty()){
sb.append(queue1.poll()+ " " );
sb.append(queue2.poll() + " ");
}
if(!queue1.isEmpty()) sb.append(queue1.poll());
System.out.println("#" + tc + " " + sb.toString());
}
}
}
큐를 2개 만들어서 단순하게 사용가능
배열을 이용해서 푸는 방법도 있음.
import java.util.*;
class Solution
{
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int tc = 1; tc <= T; tc++)
{
int n = sc.nextInt();
String[] cards = new String[n];
for(int i = 0; i < n; i++){cards[i] = sc.next();}
StringBuilder sb = new StringBuilder();
for(int i = 0; i < n/2; i++){
sb.append(cards[i] + " ");
sb.append(cards[i + (n / 2 + n % 2)] + " ");
}
if(n % 2 != 0){
sb.append(cards[n / 2]);
}
System.out.println("#" + tc + " " + sb.toString());
}
}
}
'알고리즘 - SWEA > D3' 카테고리의 다른 글
[SW expert Academy] SWEA 5456번 의석이의 세로로 말해요 자바(Java) (0) | 2023.10.30 |
---|---|
[SW expert Academy] SWEA 1234번 10일차 자바(Java) (0) | 2023.10.30 |
[SW expert Academy] SWEA 3307번 최장 증가 부분 수열 자바(Java) (0) | 2023.10.29 |
[SW expert Academy] SWEA 1873번 상호의 배틀필드 자바(Java) (1) | 2023.10.29 |
[SW expert Academy] SWEA 1221번 GNS 자바(Java) (1) | 2023.10.29 |