A triangle can be defined uniquely by its three sides. In this programming problem, you are required to define a class named Triangle
that can be used for store the information of a triangle.
Triangle
should contain three methods, __init__
, perimeter
and area
.
Note: The area of a triangle can be calculated using this formula:
, where
You can start with this template code and ensure your code can run correctly within this template:
from math import sqrt # you may use this function to calculate the area
class Triangle:
# accepts the three sides of triangle
def __init__(self, s0, s1, s2):
# your code here
# return the perimeter of this triangle.
@property
def perimeter(self):
# your code here
# return the area of this triangle.
@property
def area(self):
# your code here
# Note that the following code is for local testing purposes only. You should leave this part of code unchanged and not submit it to the OJ system.
T = int(input())
for t in range(T):
side = list(map(int, input().split()))
t = Triangle(side[0], side[1], side[2])
print(f'{t.perimeter} {t.area:.2f}')
Please note that you only have to submit the definition of the Triangle
and its content to the OJ system. The rest of the code is for your local testing purposes only.
Input Specification
The first line of the input gives the number of test cases, , and then test cases follow.
For each test case, it will consist of s single lines which contains three integers indicating the lengths of sides.
Constraints
Output Specification
For each test case, output a single line containing the perimeter and area of the given triangle.
Sample Input
2
3 4 5
6 6 6
Sample Output
12 6.00
18 15.59
Hint
object-oriented programming
Comments
助教你好:
請問這題的from math import sqrt是不是也要包含在submit的內容中?
我剛剛在沒有import的情況下只submit class Triangle,結果報出NameError;而在加上from math import sqrt這行之後就AC了
同學你好,
from math import sqrt 也需要包含在 submit 的內容裡沒有錯。