-
[SWEA] 3499. 퍼펙트 셔플PS/Java 2021. 3. 13.
퍼펙트 셔플은 카드 덱을 정확히 절반으로 나누고 나눈 것들에서 교대로 카드를 뽑아 새로운 덱을 만드는 것을 의미한다.
import java.util.Scanner; import java.util.Stack; public class 퍼펙트셔플 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int T = scanner.nextInt(); for(int tc = 1; tc <= T; tc++) { int n = scanner.nextInt(); String arr[] = new String[n]; Stack<String> stack = new Stack<>(); for(int i = 0; i < n; i++) { arr[i] = scanner.next(); } int jump; if(n % 2 == 1) { jump = n / 2 + 1; } else { jump = n / 2; } int i = 0; for(; i < n / 2; i++) { stack.push(arr[i]); stack.push(arr[i + jump]); } if(n % 2 == 1) { stack.push(arr[i]); } System.out.print("#" + tc + " "); for(int j = 0; j < stack.size(); j++) { System.out.print(stack.elementAt(j) + " "); } System.out.println(); } } }
'PS > Java' 카테고리의 다른 글
[백준] 2941. 크로아티아 알파벳 (0) 2022.03.29 [SWEA] 9229. 한빈이와 Spot Mart (0) 2021.03.13 [SWEA] 1247. [S/W 문제해결 응용] 3일차 - 최적 경로 (0) 2021.03.13 [SWEA] 1860. 진기의 최고급 붕어빵 (0) 2021.03.13 [SWEA] 2063. 중간값 찾기 (0) 2021.03.13