Triangle

View as PDF

Submit solution


Points: 10 (partial)
Time limit: 1.0s
Memory limit: 32M

Author:
Problem type
Allowed languages
Python

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:

\sqrt{s(s-a)(s-b)(s-c)}, where s = (a+b+c)/2

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, T, and then T test cases follow.

For each test case, it will consist of s single lines which contains three integers indicating the lengths of sides.

Constraints

1 \leq T \leq 100

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


  • 0
    suxen1094  commented on May 2, 2023, 12:21 a.m.

    助教你好:

    請問這題的from math import sqrt是不是也要包含在submit的內容中?

    我剛剛在沒有import的情況下只submit class Triangle,結果報出NameError;而在加上from math import sqrt這行之後就AC了


    • 0
      wadeyao  commented on May 2, 2023, 11:28 a.m.

      同學你好,

      from math import sqrt 也需要包含在 submit 的內容裡沒有錯。