Box It!

Sort by

recency

|

457 Discussions

|

  • + 0 comments
    class Box
    {
    private:
        int m_length, m_breadth, m_height;
    
    public:
        Box()
        {
            m_length = 0;
            m_breadth = 0;
            m_height = 0;
        }
        Box(int length, int breadth, int height)
        {
            m_length = length;
            m_breadth = breadth;
            m_height = height;
        }
        Box(Box &box)
        {
            m_length = box.getLength();
            m_breadth = box.getBreadth();
            m_height = box.getHeight();
        }
        int getLength()
        {
            return m_length;
        }
        int getBreadth()
        {
            return m_breadth;
        }
        int getHeight()
        {
            return m_height;
        }
        long long CalculateVolume()
        {
            return 1LL * m_length * m_breadth * m_height;
        }
        bool operator<(Box &box)
        {
            if (m_length < box.getLength())
            {
                return true;
            }
            else if (m_breadth < box.getBreadth() &&
                     m_length == box.getLength())
            {
                return true;
            }
            else if (m_height < box.getHeight() &&
                     m_breadth == box.getBreadth() &&
                     m_length == box.getLength())
            {
                return true;
            }
    
            return false;
        }
    };
    
    ostream &operator<<(ostream &out, Box &box)
    {
        return out << box.getLength() << " " << box.getBreadth() << " " << box.getHeight();
    };
    
  • + 0 comments
    class Box {
    private:
        int l = 0, b = 0, h = 0;
        
    public:
        Box() = default;
        Box(int l, int b, int h)
            : l(l), b(b), h(h) {}
        Box(const Box& other)
            : l(other.l), b(other.b), h(other.h) {}
            
        int getLength() { return l; }
        int getBreadth() { return b; }
        int getHeight() { return h; }
        
        long long CalculateVolume() { return (long)(l * b) * h; }
        
        bool operator<(Box& other) { 
            if (this->l < other.l) return true;
            else if ((this->b < other.b) && (this->l == other.l)) return true;
            else if ((this->h < other.h) && (this->l == other.l) && (this->b == other.b)) return true;
            else return false;
        }
        friend ostream& operator<<(ostream& oss, Box &box) {
            oss << box.l << " " << box.b << " " << box.h;
            return oss;
        }
    };
    
  • + 0 comments

    The test is poorly designed, as it doesn't account for its own overflows. Nor does it account for modern C++ features or CLANG features.

  • + 0 comments

    IF YOU SUCCEED AT ALL EXCEPT ONE TEST CASES, READ THIS POST: With one of the test cases, the numbers are so large that even with a line like this (which seems fine)...

    long long CalculateVolume() { return l * b * h; };

    ...The result of this, while converted to a long long eventually, is originally in integer format during multiplication, causing overflow hence producing a negative volume!

    I don't know if there's a more elegant way to do this, but I just decided to do it this way and it worked. long long CalculateVolume() { return (long long) l * (long long) b * (long long) h; };

  • + 0 comments

    What is the input format?? The problem is incomplete