POJ 2363 Blocks(我的水题之路——立方体体积和表面积,暴力)

Blocks
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5908   Accepted: 2816

Description

Donald wishes to send a gift to his new nephew, Fooey. Donald is a bit of a traditionalist, so he has chosen to send a set of N classic baby blocks. Each block is a cube, 1 inch by 1 inch by 1 inch. Donald wants to stack the blocks together into a rectangular solid and wrap them all up in brown paper for shipping. How much brown paper does Donald need?

Input

The first line of input contains C, the number of test cases. For each case there is an additional line containing N, the number of blocks to be shipped. N does not exceed 1000.

Output

Your program should produce one line of output per case, giving the minimal area of paper (in square inches) needed to wrap the blocks when they are stacked together.

Sample Input

5
9
10
26
27
100

Sample Output

30
34
82
54
130

Source

Waterloo local 2002.09.21

给出一个长方体的体积(以1的整数为单位),求这个体积的长方体的表面积的最小值。

一开始拿到这个题目,倒是一点思路也没有,这个就是将每个数字的所有的素因子组合成3个数,然后用这3个数计算出组成的长方体的表面积,这样复杂的组合是比较难进行的,不过之后看了一下数据范围最多是有1000,所以觉得可以直接暴力求解。对于体积为v的长方体的三条边a,b,c,对于a和b进行枚举,得到c = v/a/b,同时保证a/b/c均可以整除v,且a * b * c == v ,之后计算出面积,取最小值输出。

代码(1AC):
#include <cstdio>
#include <cstdlib>
#include <cmath>

int getarea(int a, int b, int c){
    return 2 * (a * b + b * c + a * c);
}

int main(void){
    int a, b, c;
    int area, v, tmp;
    int ii, casenum;

    scanf("%d", &casenum);
    for (ii = 0; ii < casenum; ii++){
        scanf("%d", &v);
        area = 1000000;
        for (a = 1; a <= sqrt(v); a++){
            if (v % a == 0){
                for (b = a; b <= sqrt(v); b++){
                    if (v % b == 0){
                        c = v / a / b;
                        if (v % c == 0 && a * b * c == v){
                            tmp = getarea(a, b, c);
                            if (tmp < area){
                                area = tmp;
                            }
                        }
                    }
                }
            }
        }
        printf("%d\n", area);
    }
    return 0;
}



你可能感兴趣的:(POJ 2363 Blocks(我的水题之路——立方体体积和表面积,暴力))