2525. 根据规则将箱子分类

发布时间 2023-10-20 10:13:05作者: DawnTraveler

1.题目介绍

2.题解(模拟)

2.1 思路

这题十分简单,唯一要注意的是length * height * width的运算结果(右边式子)默认是int类型,无法存储(不是说左边设置的变量是long long就行了,右边也要进行强制转换)
还有一个有趣的点就是这里对于\(10^4,10^9\), 不需要使用std::pow(10,4)之类的来表示,直接使用e表示法中的1e4,1e9即可

2.2 代码

class Solution {
public:
    enum BoxCategory {
        Neither,
        Bulky,
        Heavy,
        Both,
        Null
    };

    string categorizeBox(int length, int width, int height, int mass) {
        long long volume = static_cast<long long>(length) * height * width;
        BoxCategory category = Neither;
        
        //这里 (long long)length * height * width >= 1e9 强制转换也可以
        if (length >= 1e4 || width >= 1e4 || height >= 1e4 || volume >= 1e9)
            category = Bulky;
        if (mass >= 100)
            category = static_cast<BoxCategory>(category | Heavy);

        switch (category) {
            case Neither: return "Neither";
            case Bulky: return "Bulky";
            case Heavy: return "Heavy";
            case Both: return "Both";
            case Null: return "Null";
            default: return "Null";
        }
    }
};