Pages

Monday 28 January 2013

UVA - 10365 - Blocks

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {


public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer();
        int cases=Integer.parseInt(br.readLine());
        for(int i=0;i<cases;i++){
            int x=Integer.parseInt(br.readLine());
            sb.append(getArea(x)).append("\n");
        }
        System.out.print(sb);
    }

    static int getArea(int x) {
        int value = Integer.MAX_VALUE;
        for (int j = 1; j < x + 1; j++) {
            if (x % j == 0) {
                int part = x / j;
                for (int z = 1; z < part + 1; z++) {
                    if (part % z == 0) {
                        int tempArea = 2 * ( part + x / z + j * z);
                        if (tempArea < value) {
                            value = tempArea;
                        }
                    }
                }
            }
        }
        return value;
    }
}

No comments:

Post a Comment