2023/10/20

61 阅读1分钟

2525. 根据规则将箱子分类

算法掌握:

  • 模拟
  • 语法题

解题思路:

注意体积数据范围,防止爆int即可

java code:

class Solution {
    static int N = (int) 1e4, M = (int) 1e9;
    public String categorizeBox(int length, int width, int height, int mass) {
        boolean b = false, h = false;
        long v = 1L * length * width * height;
        if(length >= N || width >= N || height >= N || v >= M) b = true; 
        if(mass >= 100) h = true;
        if(b && h) return "Both";
        if(b) return "Bulky";
        if(h) return "Heavy";
        return "Neither";
    }
}

c++ code:

class Solution {
public:
    const int N = (int) 1e4, M = (int) 1e9;
    string categorizeBox(int length, int width, int height, int mass) {
        bool b = false, h = false;
        long long v = 1L * length * width * height;
        if(length >= N || width >= N || height >= N || v >= M) b = true; 
        if(mass >= 100) h = true;
        if(b && h) return "Both";
        if(b) return "Bulky";
        if(h) return "Heavy";
        return "Neither";
    }
};