poj解题报告——poj 3536 Beer Refrigerator

原题入口

poj 3536 Beer Refrigerator

题目描述

Beer Refrigerator
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5663 Accepted: 2231 Special Judge

Description

Beer Lovers Club makes regular parties. They hate warm beer, but club’s refrigerator is too small to store enough beer for the whole company. So they decided to order a special super-big beer refrigerator. The new refrigerator should be a parallelepiped a × b × c and store exactly n cubical 1 × 1 × 1 beer boxes (the club has n members). To decrease losses of cold, the total area of the surface of the refrigerator must be as small as possible.

For example, if the capacity of the refrigerator must be 12, the possible variants are:
Dimensions Surface Area
3 × 2 × 2 32
4 × 3 × 1 38
6 × 2 × 1 40
12 × 1 × 1 50

The best variant in this case is 3 × 2 × 2.

Help the beer lovers to find the optimal dimensions of their new refrigerator.

Input

The input file contains single integer number n (1 ≤ n ≤ 106) — the capacity of the refrigerator. Help the beer lovers to find the optimal dimensions of their new refrigerator.

Output

Output three integer numbers: a, b and c — the optimal dimensions of the refrigerator. If there are several solutions, output any of them.

Sample Input

#1  12
#2  13
#3  1000000

Sample Output

#1  3 2 2
#2  1 13 1
#3  100 100 100

Source
Northeastern Europe 2007, Northern Subregion

解题代码

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int n;          // 冰箱的容积
    cin >> n;
    const int nLoopLimit = (int)sqrt((double)n);
    int nValueA = 1, nValueB = 1, nValueC = 1, nSurface = 2147483647;

    for (int nA = 1; nA <= nLoopLimit; ++nA)
    {
        if (n % nA != 0)
            continue;

        const int nArea = n / nA;
        const int nLengthLimit = (int)sqrt((double)nArea);
        for(int nB = 1; nB <= nLengthLimit; ++nB)
        {
            if (nArea % nB != 0)
                continue;

            const int nC = nArea / nB;
            const int nCurSurface = nA * nB + nB * nC + nC* nA;
            if (nCurSurface >= nSurface)
                continue;

            nSurface = nCurSurface;
            nValueA = nA;
            nValueB = nB;
            nValueC = nC;
        }
    }

    cout << nValueA << " " << nValueB << " "<< nValueC << endl;
    return 0;
}

解题过程

  • 题意:啤酒俱乐部定期举行聚会,但是他们只喜凉啤酒,但是俱乐部的冰箱太小了不能放下的太多的啤酒,所有俱乐部成员打算定制一个冰箱来放啤酒,俱乐部有n个成员,所以要定制容积为n的冰箱,形状为a * b * c的长方体,现在要求计算一个实际值,使得体积为n的长方体表面积最小,因为这样可以节省表面的冰块。

  • 思路:其实这个问题就是体积一定,求表面积最小的长方体的长宽高各是多少,最简单的方法就是穷举,当然我也是暴力破解,只不过其中有一点小小的技巧,可以节省一些时间。

  • 槽点:今天这个问题看到时有点眼熟,想不起来在哪见过,直到我提交通过才发现,4年前做过:

    • 先说值的注意的,遍历的范围是sqrt(n)这样可以有效的缩短时间,同样第二个循环也可以开根号解决,因为题目中要求只要一组解就好,不用考虑颠倒顺序的情况
    • 这是第一次注意到题目中包含“Special Judge”,网上找到的解释:一个题目可以接受多种正确答案,即有多组解的时候,题目就必须被Special Judge. Special Judge程序使用输入数据和一些其他信息来判答你程序的输出,并将判答结果返回,我想就是因为这个题目的输出可以是一个解排列组合情况吧,如果他要求输出所有情况,或许就不需要“Special Judge”了。
    • 因为手贱我得到了一次WA,本来在本地测试都通过了,结果手贱的把整除的判断if (n % nA != 0)写到了for循环的结束条件中,真实不可原谅,结果还原代码后提交,一次通过。
    • 最有意思的是,当我查询提交状态时我发现这个题目我再4年前一次AC,结果今天还提交了一个WA。
    • 既然以前提交过,我决定对比一下相隔4年的代码,4年使用的C语言提交,4年后由于工作原因使用C++语言提交,思路基本一致,但是发现4年后提交的代码逻辑更加清晰,效率上有所提高。

提交结果

poj3536

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AlbertS

常来“玩”啊~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值