๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm

[SW Expert Academy] - (D3)3499. ํผํŽ™ํŠธ์…”ํ”Œ

by ์ฃผ๋ฐœ2 2020. 6. 9.
๋ฐ˜์‘ํ˜•

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWGsRbk6AQIDFAVW&categoryId=AWGsRbk6AQIDFAVW&categoryType=CODE

 

SW Expert Academy

SW ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๋„์›€์ด ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ์ปจํ…์ธ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”!

swexpertacademy.com

์ฝ”๋“œ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Solution {

	public static void main(String[] args) throws IOException {
		Scanner scan = new Scanner(System.in);
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		
		int t = scan.nextInt();
		for(int tc=1; tc<=t; tc++) {
			int N = scan.nextInt();	// ์นด๋“œ ๊ฐฏ์ˆ˜
			
			String[] cards = new String[N];
			
			// ๊ณต๋ฐฑ ๊ธฐ์ค€์œผ๋กœ ์ž˜๋ผ์„œ String ๋ฐฐ์—ด์— ๋„ฃ๊ธฐ.
			cards = bf.readLine().split(" ");
			
			for(int i=0; i<(N+1)/2; i++) {
				
				// ๋งˆ์ง€๋ง‰ ์ถœ๋ ฅํ• ๋•Œ๋งŒ ํ™€์ˆ˜, ์ง์ˆ˜ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•จ.
				if(i == (N-1)/2) {
					if(N%2 == 0) {
						System.out.print(cards[i] + " " + cards[(N+1)/2 + i]);
					} else {
						System.out.print(cards[i]);
					}
					break;
				}
				
				System.out.print(cards[i] + " " + cards[(N+1)/2 + i] + " ");
			}
		}
		
		scan.close();
		bf.close();
	}

}

ํ’€์ด

๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง€๊ณ , ๋ฐ˜์„ ๊ธฐ์ค€์œผ๋กœ ํ•˜๋‚˜ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

A B C D E F ---> A B C ||| D E F  ---> A D B E C F

 

๋”ฐ๋ผ์„œ ๋จผ์ € ํ•œ์ค„์„ ์ž…๋ ฅ๋ฐ›๊ณ (Buffered.readLine()) , split(" ")์œผ๋กœ ์ž˜๋ผ์„œ String ๋ฐฐ์—ด์— ๋„ฃ๋Š”๋‹ค.

๊ทธ ํ›„ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์ด ํ™€์ˆ˜์ธ์ง€, ์ง์ˆ˜์ธ์ง€๋งŒ ๋น„๊ตํ•˜๋ฉด ๋˜๋Š”๋ฐ...

๋งŒ์•ฝ ์ง์ˆ˜๋ผ๋ฉด --> ๋”ฑ๋งž๊ฒŒ 2๊ฐœ์”ฉ ๋–จ์–ด์ง€๋ฏ€๋กœ, ์ƒ๊ด€์ด ์—†์ง€๋งŒ ํ™€์ˆ˜๋ผ๋ฉด ๊ณ ๋ ค๋ฅผ ํ•ด์ค˜์•ผํ•œ๋‹ค.

 

๋งŒ์•ฝ ์นด๋“œ๊ฐ€ A B C D E ๋ผ๋ฉด,, A B C | D E ์ด๋ ‡๊ฒŒ 3๊ฐœ, 2๊ฐœ๋กœ ๋‚˜๋‰ ๊ฑฐ๊ณ  ..

A D / B E / C ์ด๋Ÿฐ์‹์œผ๋กœ ๋งˆ์ง€๋ง‰ (i == (N-1)/2) ์—๋Š” ํ•œ๊ฐœ๋งŒ ์ถœ๋ ฅ์„ ํ•ด์•ผํ•œ๋‹ค.

 

๋”ฐ๋ผ์„œ ์ง์ˆ˜์ธ์ง€ ํ™€์ˆ˜์ธ์ง€์— ๋”ฐ๋ผ ์ถœ๋ ฅ์„ ๋‹ค๋ฅด๊ฒŒ ํ•˜๊ณ ,

cards ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค ๊ฐ’์œผ๋กœ๋Š”.. 

 

0 1 2 3 4 5 

A B C D E F

 

์ด๋ ‡๊ฒŒ ์žˆ์„๋•Œ, (0, 3) , (1, 4) , (2, 5) ์ด๋Ÿฐ์‹์œผ๋กœ ์ถœ๋ ฅ์„ ํ•ด์ค˜์•ผํ•œ๋‹ค.

๋”ฐ๋ผ์„œ i์™€ (N+1)/2 + 1 ์„ ์ธ๋ฑ์Šค๋กœ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€