2525. 根据规则将箱子分类

1.题目介绍

2.题解(模拟)

2.1 思路

这题十分简单,唯一要注意的是length * height * width的运算结果(右边式子)默认是int类型,无法存储(不是说左边设置的变量是long long就行了,右边也要进行强制转换)
还有一个有趣的点就是这里对于104,109, 不需要使用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";
        }
    }
};

posted @   DawnTraveler  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示