Problem E: 正方形、长方形、立方体
来源:互联网 发布:smali语法编程 编辑:程序博客网 时间:2024/05/13 07:28
Problem E: 正方形、长方形、立方体
Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1804 Solved: 1273
[Submit][Status][Web Board]
Description
给出正方形(Square)、长方形(Rectangle)、立方体(Cuboid)的边长,求周长、面积、体积。
Square类只需存一条边长,构造函数产生一条输出,有边长、周长、面积的函数。
Rectangle类需存长和宽,若从Square类派生而来,因此只需增加一条边,构造函数产生一条输出,有长、宽、周长、面积的函数。
Cuboid类需存长宽高,若从Rectangle类派生而来,因此也只增加一条边,构造函数产生一条输出,有长、宽、高、周长、面积、体积的函数。它的周长定义为所有棱长之和。
-----------------------------------------------------------------------------
请仔细阅读append.cc代码,并设计好正方形、长方形、立方体派生关系,使main()函数能够运行并得到正确的输出。
Input
输入分为三部分,每一部分都已一个整数n开始,表示后面有n组测试数据。
在第一部分测试数据中,每组是一个整数,表示正方形的边长。
在第二部分测试数据中,每组是两个整数,表示长方形的长和宽。
在第三部分测试数据中,每组是三个整数, 表示立方体的长和宽。
Output
每组测试数据对应的输出为两部分,前面是构造函数的输出,最后是输出图形的信息,包括长宽高、周长、面积、体积等信息,格式见sample;
Sample Input
1413 413 4 6
Sample Output
Construct Square (4)A Square length 4, Perimeter 16, Area 16=========================Construct Square (3)Construct Rectangle (3, 4)A Rectangle length 3, width 4, Perimeter 14, Area 12=========================Construct Square (3)Construct Rectangle (3, 4)Construct Cuboid (3, 4, 6)A Cuboid length 3, width 4, height 6, Perimeter 52, Area 108, Volume 72
HINT
Append Code
append.c, append.cc,
#include <iostream>using namespace std;class Square{public: int l; Square(int l=0):l(l){cout<<"Construct Square ("<<l<<")"<<endl;} int length(){return l;} int perimeter(){return 4*l;} int area(){return l*l;}};class Rectangle:public Square{public: int w; Rectangle(int _l,int _w):Square(_l) { w = _w; cout<<"Construct Rectangle ("<<l<<", "<<w<<")"<<endl; } int width(){return w;} int perimeter(){return 2*(l+w);} int area(){return l*w;}};class Cuboid:public Rectangle{public: int h; Cuboid(int l,int w,int h):Rectangle(l,w),h(h){cout<<"Construct Cuboid ("<<l<<", "<<w<<", "<<h<<")"<<endl;} int perimeter(){return 4*(l+w+h);} int height(){return h;} int area() {return 2*(l*w+w*h+h*l);} int volume(){return l*w*h;}};int main(){ int cases, l, w, h; cin >> cases; for(int i = 1; i <= cases; ++i) { cin >> l; Square squa(l); cout << "A Square length " << squa.length() << ", "; cout << "Perimeter " << squa.perimeter() << ", "; cout << "Area " << squa.area() << endl; } cout << "=========================" << endl; cin >> cases; for(int i = 1; i <= cases; ++i) { cin >> l >> w; Rectangle rect(l, w); cout << "A Rectangle length " << rect.length() << ", width " << rect.width() << ", "; cout << "Perimeter " << rect.perimeter() << ", "; cout << "Area " << rect.area() << endl; } cout << "=========================" << endl; cin >> cases; for(int i = 1; i <= cases; ++i) { cin >> l >> w >> h; Cuboid cubo(l, w, h); cout << "A Cuboid length " << cubo.length() << ", width " << cubo.width() << ", height " << cubo.height() << ", "; cout << "Perimeter " << cubo.perimeter() << ", "; cout << "Area " << cubo.area() << ", "; cout << "Volume " << cubo.volume() << endl; }}
0 0
- Problem E: 正方形、长方形、立方体
- Problem H: 正方形、长方形、立方体
- Problem C: 正方形、长方形、立方体
- Problem F: 正方形、长方形、立方体
- 问题 F: 正方形、长方形、立方体
- java 求正方形的面积,长方形的面积和立方体的表面积
- 也谈"正方形不是长方形"
- CSS3实现正方形立方体旋转
- 长方形可用最少正方形来代替
- 把长方形的图片截取成正方形
- 是否能够组成长方形(不包括正方形)
- 计算圆,正方形和长方形的面积
- 面向对象方法的困境--正方形不能继承自长方形?
- n*m棋盘的长方形个数和正方形个数
- 面向对象的疑惑,java设计正方形,长方形
- iOS将长方形的图片截成正方形显示
- 计算长方形或正方形的周长和面积
- [从头学数学] 第44节 长方形和正方形
- 500 OOPS: vsftpd: refusing to run with writable root inside chroot ()
- Android安卓---Hellword 简单提示框
- Problem D: 模板是个好东西
- 手机相机和相册:仿微信自定义相册
- 机器学习工程师必知的十大算法
- Problem E: 正方形、长方形、立方体
- lintode(52)下一个排列
- linux下安装第二个tomcat
- 数据预处理与特征选择
- 关于数组的基本数据类型和引用类型
- 块级元素和行内元素有哪些?
- mysql 5.7.18解压版安装error总汇
- Problem D: 数组类(II)
- retrofit配合使用protobuf3