列表

详情


CPP66. 求长方体表面积

描述

现在有长方形类(rectangle),请以此为基类构建长方体类(cuboid)并实现求表面积的area方法。

输入描述

输入三个整数分别表示长宽高。

输出描述

第一行输出一个整数表示长方体的底面积。
第二行输出一个整数表示长方体的表面积。

示例1

输入:

3 2 1

输出:

6
22

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 3ms, 内存消耗: 392KB, 提交时间: 2022-04-07

#include<bits/stdc++.h>
using namespace std;
class rectangle
{
	private:
		int length,width;
	public:
		rectangle(int x,int y){
			length=x;
			width=y;
		}
		void set(int x,int y){
			length=x;
			width=y;
		}
		int getlength(){
			return length;
		}
		int getwidth(){
			return width;
		}
		int area()
        {
			return length*width;
		}
};
class cuboid:public rectangle
{
	private:
		int height;
	public:
		// write your code here...
		cuboid(int length,int width,int height ):rectangle(length,width)
        {
            this -> height=height;
        }
        int area()
        {
         //int s1 = height*rectangle::getlength();
         //int s2 = height*rectangle::getwidth();
         // return (s1+s2+rectangle::area())*2;
         
           int s1 = height*rectangle::getlength();
           int s2 = height*rectangle::getwidth();
           int s3 = rectangle::area();
           return (s1+s2+s3)*2;
           
       
        }
};
int main(){
	int x,y,z;
	cin>>x>>y>>z;
	cuboid a(x,y,z);
	cout<<a.rectangle::area()<<'\n'<<a.area();
	return 0;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 392KB, 提交时间: 2022-03-12

#include<bits/stdc++.h>
using namespace std;
class rectangle{
	private:
		int length,width;
	public:
		rectangle(int x,int y){
			length=x;
			width=y;
		}
		void set(int x,int y){
			length=x;
			width=y;
		}
		int getlength(){
			return length;
		}
		int getwidth(){
			return width;
		}
		int area(){
			return length*width;
		}
};
class cuboid:public rectangle{
	private:
		int height;
	public:
		// write your code here...
		cuboid(int a,int b,int c):rectangle(a,b),height(c){}
        int area(){
            return getlength()*getwidth()*2+height*getlength()*2+height*getwidth()*2;
        }
};
int main(){
	int x,y,z;
	cin>>x>>y>>z;
	cuboid a(x,y,z);
	cout<<a.rectangle::area()<<'\n'<<a.area();
	return 0;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 396KB, 提交时间: 2022-08-06

#include<bits/stdc++.h>
using namespace std;
class rectangle{
	private:
		int length,width;
	public:
		rectangle(int x,int y){
			length=x;
			width=y;
		}
		void set(int x,int y){
			length=x;
			width=y;
		}
		int getlength(){
			return length;
		}
		int getwidth(){
			return width;
		}
		int area(){
			return length*width;
		}
};
class cuboid:public rectangle{
	private:
		int height;
	public:
		// write your code here...
		cuboid(int x, int y, int z): rectangle(x,y){
            this->height = z;
        }
        int getHeight(){
            return height;
        }
        int area(){
            return 2 * (rectangle::area() + rectangle::getlength() * height + rectangle::getwidth() * height);
        }
};
int main(){
	int x,y,z;
	cin>>x>>y>>z;
	cuboid a(x,y,z);
	cout<<a.rectangle::area()<<'\n'<<a.area();
	return 0;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 396KB, 提交时间: 2022-08-04

#include<bits/stdc++.h>
using namespace std;
class rectangle{
	private:
		int length,width;
	public:
		rectangle(int x,int y){
			length=x;
			width=y;
		}
		void set(int x,int y){
			length=x;
			width=y;
		}
		int getlength(){
			return length;
		}
		int getwidth(){
			return width;
		}
		int area(){
			return length*width;
		}
};
class cuboid:public rectangle{
	private:
		int height;
	public:
		// write your code here...
		cuboid(int x, int y, int z):rectangle(x,y),height(z){}
    int area()
    {
        return 2 * (getlength() * getwidth() + getwidth() * height + height * getlength());
    }
};
int main(){
	int x,y,z;
	cin>>x>>y>>z;
	cuboid a(x,y,z);
	cout<<a.rectangle::area()<<'\n'<<a.area();
	return 0;
}

C++ 解法, 执行用时: 3ms, 内存消耗: 396KB, 提交时间: 2022-08-03

#include<bits/stdc++.h>
using namespace std;
class rectangle{
	private:
		int length,width;
	public:
		rectangle(int x,int y){
			length=x;
			width=y;
		}
		void set(int x,int y){
			length=x;
			width=y;
		}
		int getlength(){
			return length;
		}
		int getwidth(){
			return width;
		}
		int area(){
			return length*width;
		}
};
class cuboid:public rectangle{
	private:
		int height;
	public:
		// write your code here...
    cuboid(int x,int y,int z):rectangle(x, y){
        this->height = z;
    }
    int area(){
        return (this->rectangle::area()+this->height*this->getlength()+this->height*this->getwidth())*2;
    }
		
};
int main(){
	int x,y,z;
	cin>>x>>y>>z;
	cuboid a(x,y,z);
	cout<<a.rectangle::area()<<'\n'<<a.area();
	return 0;
}

上一题