题目内容:
请定义一个Cylinder类,具体要求为:(1)每个Cylinder类对象可以存储一个圆柱体(包括半径和高);(2)具有用于初始化半径和高的构造方法;(3)具有计算圆柱体体积的方法GetVolume。
import math
#请在此处写出Cylinder类定义的代码(提示:计算体积时使用math.pi作为圆周率)
if __name__=='__main__':
r=eval(input()) #输入半径
h=eval(input()) #输入高
c=Cylinder(r,h) #创建Cylinder对象
print('radius:%.2f,height:%.2f'%(c.r,c.h)) #输出半径和高
print('volume:%.2f'%c.GetVolume()) #输出体积
输入格式:
分两行分别输入半径和高
输出格式:
第一行输出半径和高,第二行输出体积
输入样例:
3 5.2
输出样例:
radius:3.00,height:5.20 volume:147.03
时间限制:500ms内存限制:32000kb
代码:
import math
class Cylinder:
def __init__(self, radius, height):
self.r = radius
self.h = height
def GetVolume(self):
return math.pi * self.r**2 * self.h
if __name__=='__main__':
r=eval(input()) #输入半径
h=eval(input()) #输入高
c=Cylinder(r,h) #创建Cylinder对象
print('radius:%.2f,height:%.2f'%(c.r,c.h)) #输出半径和高
print('volume:%.2f'%c.GetVolume()) #输出体积
用例测试结果 | 运行时间 | 占用内存 | 提示 | 得分 |
---|---|---|---|---|
用例1通过 | 30ms | 4624kb | 1 | |
用例2通过 | 32ms | 4516kb | 1 |
本次得分/总分:2.00/2.00分