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

[SW Expert Academy] - (D4)7829. ๋ณด๋ฌผ์™• ํƒœํ˜

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

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

 

SW Expert Academy

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

swexpertacademy.com

์ฝ”๋“œ

import java.util.Arrays;
import java.util.Scanner;

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 p = scan.nextInt();	// ์•ฝ์ˆ˜์˜ ๊ฐฏ์ˆ˜
			int[] arr = new int[p];
			int ans = 0;
			
			for(int i=0; i<p; i++) {
				arr[i] = scan.nextInt();
			}
			
			Arrays.sort(arr);
			
			/*
			 * ์•ฝ์ˆ˜์˜ ๊ฐฏ์ˆ˜๊ฐ€ 1๊ฐœ๋ฉด ๊ทธ ์ˆ˜๋Š” ์ œ๊ณฑ์ˆ˜์ด๊ณ , 
			 * 1๊ฐœ๊ฐ€ ์•„๋‹ˆ๋ฉด ๊ฐ€์žฅ ์ž‘์€๊ฐ’ * ๊ฐ€์žฅ ํฐ๊ฐ’์ด ๊ตฌํ•˜๋Š” ์ˆ˜๊ฐ€ ๋จ.
			 */
			if(arr.length == 1) {
				ans = arr[0] * arr[0];
			} else {
				ans = arr[0] * arr[arr.length-1];
			}
			
			System.out.println("#" + tc + " " + ans);
		}
		
		scan.close();
	}

}

ํ’€์ด

๋ฐฐ์—ด์„ ์ •๋ ฌํ•˜๊ณ  mix * max ํ•˜๋ฉด ๋˜๋Š” ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์ด๋‹ค.

4, 9, 16 ๋“ฑ ์ œ๊ณฑ์ˆ˜์˜ ๊ฒฝ์šฐ ์•ฝ์ˆ˜๊ฐ€ 1๊ฐœ(1, n์ œ์™ธ)์ด๋ฏ€๋กœ 

๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ 1์ด๋ฉด ๊ทธ ๊ฐ’์„ ์ œ๊ณฑํ•˜๋ฉด ๋œ๋‹ค.

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€