Surface Area of 3D Shapes

On a N * N grid, we place some 1 * 1 * 1 cubes.

Each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell (i, j).

Return the total surface area of the resulting shapes.

 

Example 1:

Input: [[2]]
Output: 10

Example 2:

Input: [[1,2],[3,4]]
Output: 34

Example 3:

Input: [[1,0],[0,2]]
Output: 16

Example 4:

Input: [[1,1,1],[1,0,1],[1,1,1]]
Output: 32

Example 5:

Input: [[2,2,2],[2,1,2],[2,2,2]]
Output: 46

 

Note:

  • 1 <= N <= 50
  • 0 <= grid[i][j] <= 50

题目理解:

用二维数组表示一个三维图形,计算这个三维图形的表面积

解题思路:

计算每一个(x,y)坐标上小方块的表面积,如果这个小方块的上面或者左面有小方块,那么就减去遮挡的部分

代码如下:

class Solution {
    public int surfaceArea(int[][] grid) {
    	int res = 0;
        int row = grid.length;
        if(row == 0)
        	return 0;
        int col = grid[0].length;
        for(int i = 0; i < row; i++) {
        	for(int j = 0; j < col; j++) {
        		if(grid[i][j] == 0)
        			continue;
        		res +=  4 * grid[i][j] + 2;
        		if(i > 0) {
        			res -= Math.min(grid[i][j], grid[i - 1][j]) * 2;
        		}
        		if(j > 0) {
        			res -= Math.min(grid[i][j], grid[i][j - 1]) * 2;
        		}
        	}
        }
        return res;
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个可能的实现,其中球、圆柱和圆锥都是基于一个名为“Shape”的基类派生的: ```cpp #include <iostream> #include <cmath> using namespace std; class Shape { protected: double radius; public: Shape(double r = 0) : radius(r) {} virtual double getArea() const = 0; virtual double getVolume() const = 0; virtual void print() const = 0; }; class Sphere : public Shape { public: Sphere(double r) : Shape(r) {} double getArea() const { return 4 * M_PI * radius * radius; } double getVolume() const { return 4.0 / 3.0 * M_PI * radius * radius * radius; } void print() const { cout << "Sphere with radius " << radius << ":" << endl; cout << "Surface area = " << getArea() << endl; cout << "Volume = " << getVolume() << endl; } }; class Cylinder : public Shape { private: double height; public: Cylinder(double r, double h) : Shape(r), height(h) {} double getArea() const { return 2 * M_PI * radius * height + 2 * M_PI * radius * radius; } double getVolume() const { return M_PI * radius * radius * height; } void print() const { cout << "Cylinder with radius " << radius << " and height " << height << ":" << endl; cout << "Surface area = " << getArea() << endl; cout << "Volume = " << getVolume() << endl; } }; class Cone : public Shape { private: double height; public: Cone(double r, double h) : Shape(r), height(h) {} double getArea() const { return M_PI * radius * (radius + sqrt(height*height + radius*radius)); } double getVolume() const { return 1.0 / 3.0 * M_PI * radius * radius * height; } void print() const { cout << "Cone with radius " << radius << " and height " << height << ":" << endl; cout << "Surface area = " << getArea() << endl; cout << "Volume = " << getVolume() << endl; } }; int main() { Shape* shapes[3]; shapes[0] = new Sphere(2); shapes[1] = new Cylinder(2, 4); shapes[2] = new Cone(2, 4); for (int i = 0; i < 3; i++) { shapes[i]->print(); cout << endl; } return 0; } ``` 这个程序的输出将是: ``` Sphere with radius 2: Surface area = 50.2655 Volume = 33.5103 Cylinder with radius 2 and height 4: Surface area = 75.3982 Volume = 50.2655 Cone with radius 2 and height 4: Surface area = 40.8407 Volume = 16.7552 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值