๊ธฐ๋ก๋ฐฉ

BOJ_3595 : ๋งฅ์ฃผ ๋ƒ‰์žฅ๊ณ  ๋ณธ๋ฌธ

CodingTest/Java

BOJ_3595 : ๋งฅ์ฃผ ๋ƒ‰์žฅ๊ณ 

Soom_1n 2022. 12. 5. 22:17

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

3595๋ฒˆ: ๋งฅ์ฃผ ๋ƒ‰์žฅ๊ณ 

๋งฅ์ฃผ๋ฅผ ์ข‹์•„ํ•˜๋Š” ์ฐฝ์˜์ด๋Š” ๋ƒ‰์žฅ๊ณ ์— ๋งฅ์ฃผ๋ฅผ ๋ณด๊ด€ํ•œ๋‹ค. ์ผ๋ฐ˜ ๋ƒ‰์žฅ๊ณ ์— ์Œ์‹๊ณผ ๋งฅ์ฃผ๋ฅผ ํ•จ๊ป˜ ๋ณด๊ด€ํ•˜๋‹ค๋ณด๋‹ˆ ์ฐฝ์˜์ด์˜ ๋ƒ‰์žฅ๊ณ ์—๋Š” ๋งฅ์ฃผ๋ฅผ ๋„ฃ์„ ๊ณณ์ด ์ ์  ์—†์–ด์ง€๊ณ  ์žˆ์—ˆ๋‹ค. ์ฐฝ์˜์ด๋Š” ๋งฅ์ฃผ ์ „์šฉ ๋ƒ‰์žฅ

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ๊ฐ€๋กœ, ์„ธ๋กœ, ๋†’์ด ์„ธ ์ˆ˜๋ฅผ ๊ณฑํ•ด์„œ n์„ ๋งŒ๋“ค ๋•Œ ๊ฐ€์žฅ ์ž‘์€ ๊ฒ‰๋„“์ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long min = 987654321;
        int[] arr = new int[3];

        for (int a = 1; a <= n; a++) {
            for (int b = 1; b <= a; b++) {

                if (a * b > n) break;

                for (int c = 1; c <= b; c++) {

                    if (a * b * c > n) break;
                    if (a * b * c == n) {
                        long sum = a * b + b * c + c * a;
                        if (sum < min) {
                            min = sum;
                            arr[0] = a;
                            arr[1] = b;
                            arr[2] = c;
                        }
                    }
                }
            }
        }
        System.out.println(arr[0] + " " + arr[1] + " " + arr[2]);
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • a, b, c๋ฅผ ์กฐํ•ฉํ•ด n์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ชจ๋‘ ๋น„๊ตํ•ด์•ผ ํ•œ๋‹ค.
    • ์ตœ์†Œ ํฌ๊ธฐ์˜ ๊ฒ‰๋„“์ด์™€ ๊ทธ๋•Œ์˜ a, b, c๋ฅผ ์ €์žฅํ•˜๊ณ  ๋งˆ์ง€๋ง‰์— ์ถœ๋ ฅํ•œ๋‹ค.
  • ๊ฒ‰๋„“์ด์˜ ํฌ๊ธฐ๊ฐ€ ์ปค์งˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ longํ˜•์œผ๋กœ ๊ณ„์‚ฐํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ๊ฒ‰๋„“์ด์˜ ํฌ๊ธฐ๋ฅผ long์œผ๋กœ ๋‹ด์•„์•ผํ•˜๋Š” ๊ฑด ์˜ˆ์ƒํ–ˆ์ง€๋งŒ, ์กฐ๊ฑด๋ฌธ ์•ˆ์— ๋ฐ”๋กœ ๋„ฃ์–ด์„œ int๋กœ ๊ณ„์‚ฐ๋˜์„œ ํ‹€๋ ธ์—ˆ๋‹ค.
    • a, b, c๊ฐ€ intํ˜•์ด๋”๋ผ๋„ longํ˜• ๋ณ€์ˆ˜์— ๋‹ด์•˜์–ด์•ผ ํ–ˆ๋‹ค.

728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_11971 : ์†๋„ ์œ„๋ฐ˜  (0) 2022.12.06
BOJ_24389 : 2์˜ ๋ณด์ˆ˜  (0) 2022.12.05
BOJ_4158 : CD  (0) 2022.12.01
BOJ_3447 : ๋ฒ„๊ทธ์™•  (0) 2022.12.01
BOJ_2520 : ํŒฌ์ผ€์ดํฌ ์‚ฌ๋ž‘  (0) 2022.12.01