본문 바로가기
Problem Solving/Baekjoon

[백준] 14753 MultiMax - Brute Force / Java

by graycode 2023. 7. 26.

 문제 링크

 

14753번: MultiMax

There are n cards, each with an integer on it where two or more cards can have the same integer. From these cards, we want to select two or three cards such that the product of numbers on the selected cards is maximum. For example, assume that there are 6

www.acmicpc.net

 

 풀이 과정

 

 풀이 코드

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int n = Integer.parseInt(br.readLine());
        int[] arr = new int[n];

        StringTokenizer st = new StringTokenizer(br.readLine());
        for (int i = 0; i < n; i++)
            arr[i] = Integer.parseInt(st.nextToken());

        Arrays.sort(arr);

        int max = Math.max(arr[--n] * arr[n - 1] * arr[n - 2], Math.max(arr[n] * arr[1] * arr[0], Math.max(arr[n] * arr[n - 1], arr[0] * arr[1])));

        bw.write(String.valueOf(max));
        bw.flush();
    }

}

댓글