hwangnk1004 / Algorithm

0 stars 0 forks source link

StepByStep #맥주파티 #250

Open hwangnk1004 opened 4 years ago

hwangnk1004 commented 4 years ago

import java.util.Scanner;

public class Main { public static void main(String args[]) {

    Scanner scanner = new Scanner(System.in);

    int n = scanner.nextInt();
    long sum = 200000000;

    for (int a = 1; a * a <= n; a++) {
        for (int b = 1; b <= n; b++) {
            if (a * b > n) {
                break;
            }
            for (int c = 1; c <= n; c++) {
                if (a * b * c > n) {
                    break;
                } else if (a * b * c == n) {

                    sum = Math.min(sum, a * b * 2 + a * c * 2 + b * c * 2);

                    System.out.println(a + " " + b + " " + c);

                }

            }
        }
    }

    System.out.println(n);
}

}

hwangnk1004 commented 4 years ago
package main2;

import java.util.Scanner;

public class Main {
    public static void main(String args[]) {

        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int count = 0;

        for (int i = 1; i * i <= n; i++) {
            if (n % i == 0) {
                count++;
            }

        }
        if (count == 1) {
            System.out.println(1 + " " + 1 + " " + n);
            return;
        }

        long x = 0;
        long y = 0;
        long z = 0;
        long sum2 = 0;
        sum2 = 2000000000;
        for (long i = 1; i*i*i <= n; i++) {
            for (long j = 1; j*j <= n; j++) {
                long k = n / (i * j);
                if (i * j * k == n) {
                    long sum = i * j * 2 + j * k * 2 + i * k * 2;

                    if (sum2 > sum) {
                        sum2 = sum;
                        x = i;
                        y = j;
                        z = k;
                    }
                }
            }
        }

        if(x!=0){
            System.out.println(x + " " + y + " " + z);
            return;
        }

        long sum3=0;
        for (long i = 1; i * i <= n; i++) {
            if (i * i == n) {
                sum3 = 1 * i * 2 + 1 * i * 2 + i * i * 2;
                if (sum2 < sum3) {
                    System.out.println(x + " " + y + " " + z);
                    return;
                } else {
                    System.out.println(1 + " " + i + " " + i);
                    return;
                }
            }
        }
        System.out.println(x + " " + y + " " + z);

    }
}