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

[SW Expert Academy] - (D3)5948. ์ƒˆ์ƒ˜์ด์˜ 7-3-5 ๊ฒŒ์ž„

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

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

 

SW Expert Academy

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

swexpertacademy.com

์ฝ”๋“œ

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Solution {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		int T = scan.nextInt();
		for(int tc=1; tc<=T; tc++) {
			int[] arr = new int[7];	// 7๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ์ •์ˆ˜
			for(int i=0; i<arr.length; i++) {
				arr[i] = scan.nextInt();
			}
			
			Set<Integer> hs = new HashSet<Integer>();
			
			for(int i=0; i<arr.length-2; i++) {
				for(int j=i+1; j<arr.length-1; j++) {
					for(int k=j+1; k<arr.length; k++) {
						hs.add(arr[i] + arr[j] + arr[k]);
					}
				}
			}
			
			// HashSet -> ArrayList ๋ณ€ํ™˜(์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด)
			List<Integer> list = new ArrayList<Integer>(hs);
			Collections.sort(list);
			System.out.println("#" + tc + " " + list.get(list.size()-5));
		}
		
		scan.close();
	}

}

ํ’€์ด

๋ชจ๋“  ๊ฒฝ์šฐ์˜์ˆ˜๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ์™„์ „ํƒ์ƒ‰์œผ๋กœ ํ’€์—ˆ๋‹ค.

์กฐํ•ฉ์˜ ๋ฌธ์ œ๋กœ๋„ ํ’€์ˆ˜์žˆ๊ฒ ์ง€๋งŒ, ์•„์ง ์ˆœ์—ด ์กฐํ•ฉ๋ถ€๋ถ„์€ ๊ณต๋ถ€๋ฅผ ์•ˆํ–ˆ๊ธฐ์—, ์™„์ „ํƒ์ƒ‰์œผ๋กœ ํ•ด๊ฒฐํ–ˆ๋‹ค.

 

์„œ๋กœ๋‹ค๋ฅธ 7๊ฐœ์˜ ์ •์ˆ˜๋“ค์ค‘ 3๊ฐœ์˜ ์ •์ˆ˜์˜ ํ•ฉ์„ ๋ชจ๋‘ ๊ตฌํ•ด์„œ HashSet์— ๋„ฃ๋Š”๋‹ค.

 

HashSet์€ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•ด์ฃผ๋ฏ€๋กœ, ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ 3๊ฐœ์˜ ์ •์ˆ˜์˜ ํ•ฉ๋“ค ์ค‘์—์„œ ๋™์ผํ•œ ๊ฐ’์„ ์ œ๊ฑฐ๋œ๋‹ค.

 

๊ทธ ํ›„ ์˜ค๋ฆ„์ฐจ์ˆœ์ •๋ ฌ์„ ํ•˜๊ธฐ ์œ„ํ•ด HashSet์„ ArrayList๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๊ณ , ์ •๋ ฌ์„ ํ•œ๋‹ค.

 

๊ทธ๋Ÿผ ๋์—์„œ 5๋ฒˆ์งธ๊ฐ’์ด 5๋ฒˆ์งธ๋กœ ํฐ ์ˆ˜์ด๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€