Python Basic: Tập thể dục-60 với giải pháp
Viết một chương trình Python để tính toán hạ huyết áp của một tam giác góc phải.
Từ Wikipedia, một tam giác bên phải hoặc hình tam giác góc phải, hoặc chính thức hơn là một tam giác trực giao, là một tam giác trong đó một góc là một góc vuông. Mối quan hệ giữa các cạnh và góc của một tam giác vuông là cơ sở cho lượng giác. Phía đối diện góc bên phải được gọi là hypotenuse. Nếu chiều dài của cả ba cạnh của một tam giác vuông là số nguyên, thì tam giác được cho là một tam giác Pythagore và độ dài bên của nó được gọi chung là một bộ ba Pythagore.
A right triangle or right-angled triangle, or more formally an orthogonal triangle, is a triangle in which one angle is a right angle. The relation
between the sides and angles of a right triangle is the basis for trigonometry. The side opposite the right angle is called the hypotenuse. If the lengths of all three sides of a right triangle are integers, the triangle is said to be a Pythagorean triangle and its side lengths are collectively known as a Pythagorean triple.
Trình bày bằng hình ảnh:
Giải pháp mẫu-1:
Mã Python:
from math import sqrt
print["Input lengths of shorter triangle sides:"]
a = float[input["a: "]]
b = float[input["b: "]]
c = sqrt[a**2 + b**2]
print["The length of the hypotenuse is:", c ]
Đầu ra mẫu:
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.0
Trực quan hóa thực thi mã Python:
Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực hiện chương trình đã nói:
Giải pháp mẫu-2:
Mã Python:
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
Đầu ra mẫu:
5.0 5.622277118748239
Flowchart:
Trực quan hóa thực thi mã Python:
Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực hiện chương trình đã nói:
Giải pháp mẫu-2:
Mã Python:
Trình chỉnh sửa mã Python: Write a Python program to convert height [in feet and inches] to centimeters.
Next: Write a Python program to convert the distance [in feet] to inches, yards, and miles.
Python: Lời khuyên trong ngày
Giải nén các biến bằng toán tử splat:
>>> def test[x, y, z]: >>> print[x, y, z] >>> res = test[*[10, 20, 30]] 10 20 30 >>> res = test[**{'x': 1, 'y': 2, 'z': 3} ] 10 20 30
Phương pháp toán học
Thí dụ
Tìm hypotenuse của một tam giác góc phải, nơi vuông góc và cơ sở được biết đến:
#Trình thư viện toán học Mathimport Toán học
import math
#Set vuông góc và cơ sở parendicular = 10base = 5
parendicular = 10
base = 5
#in hypotenuse của một hình tam giác góc phải [math.hypot [parendicular, cơ sở]]]
print[math.hypot[parendicular, base]]
Hãy tự mình thử »
Định nghĩa và cách sử dụng
Phương pháp math.hypot[]
trả về định mức Euclide. Định mức Euclidian là khoảng cách từ nguồn gốc đến tọa độ được đưa ra.
Python 3.8 trước đó, phương pháp này chỉ được sử dụng để tìm ra hypotenuse của một tam giác góc phải: SQRT [x*x + y*y].
Từ Python 3.8, phương pháp này cũng được sử dụng để tính định mức Euclide. Đối với các trường hợp N chiều, các tọa độ được thông qua được coi là giống như [x1, x2, x3, ..., xn]. Vì vậy, chiều dài Euclide từ gốc được tính bằng sqrt [x1*x1 + x2*x2 + x3*x3 .... xn*xn].
Cú pháp
math.hypot [x1, x2, x3, ..., xn]
Giá trị tham số
x1, x2, x3, ..., xn | Yêu cầu. Hai hoặc nhiều điểm đại diện cho tọa độ |
Chi tiết kỹ thuật
Giá trị float , đại diện cho khoảng cách Euclide từ nguồn gốc cho đầu vào n hoặc hạ huyết áp của một tam giác góc phải cho hai đầu vào |
Từ 3.8: Cũng hỗ trợ các điểm N chiều. Các phiên bản trước chỉ hỗ trợ các điểm hai chiều |
Nhiều ví dụ hơn
Thí dụ
Tìm hypotenuse của một tam giác góc phải, nơi vuông góc và cơ sở được biết đến:
#Trình thư viện toán học Mathimport Toán học
import math
#Set vuông góc và cơ sở parendicular = 10base = 5
print[math.hypot[10, 2, 4, 13]]
print[math.hypot[4, 7, 8]]
print[math.hypot[12, 14]]
Phương pháp toán học
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Examples:
Bàn luận side1 = 3, side2 = 4
Output: 5.00
32 + 42 = 52
Input: side1 = 12, side2 = 15
Output: 19.21
Với hai mặt khác của một tam giác góc phải, nhiệm vụ là tìm thấy nó hạ huyết áp. Pythagoras theorem states that the square of hypotenuse of a right angled
triangle is equal to the sum of squares of the other two sides.
Below is the implementation of the above approach:
C++
#include
#include
#include
Đầu vào: Side1 = 3, Side2 = 4 & nbsp; đầu ra: 5,00 & nbsp; 32 + 42 = 52Input: side1 = 12, side2 = 15 & nbsp; đầu ra: 19,21 & nbsp; & nbsp;
Cách tiếp cận: Định lý Pythagoras nói rằng hình vuông của các hình tam giác góc phải bằng với tổng hình vuông của hai mặt khác .Below là việc thực hiện phương pháp trên: & nbsp;
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.09
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.00
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.01
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.02
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.03
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.04
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.03
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.06
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.03
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.08
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
8def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
0Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.03
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
2def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
3def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
4Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.09
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
0def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
6 def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
7def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
9 5.0 5.6222771187482390
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
05.0 5.6222771187482398
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
0>>> def test[x, y, z]: >>> print[x, y, z] >>> res = test[*[10, 20, 30]] 10 20 30 >>> res = test[**{'x': 1, 'y': 2, 'z': 3} ] 10 20 300
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
8def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
0def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
9 5.0
5.622277118748239
4
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
05.0 5.6222771187482396
Java
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
0Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.09
>>> def test[x, y, z]: >>> print[x, y, z] >>> res = test[*[10, 20, 30]] 10 20 30 >>> res = test[**{'x': 1, 'y': 2, 'z': 3} ] 10 20 302
>>> def test[x, y, z]: >>> print[x, y, z] >>> res = test[*[10, 20, 30]] 10 20 30 >>> res = test[**{'x': 1, 'y': 2, 'z': 3} ] 10 20 303
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
0>>> def test[x, y, z]: >>> print[x, y, z] >>> res = test[*[10, 20, 30]] 10 20 30 >>> res = test[**{'x': 1, 'y': 2, 'z': 3} ] 10 20 305
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.03
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.04
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.03
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.06
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.03
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.08
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
0def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
8math.hypot[]
4
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.03
math.hypot[]
6def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
0Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.09
math.hypot[]
4
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
6 def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
7math.hypot[]
4#include
7#include
8#include
9
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
0float
3 >>> def test[x, y, z]: >>> print[x, y, z] >>> res = test[*[10, 20, 30]] 10 20 30 >>> res = test[**{'x': 1, 'y': 2, 'z': 3} ] 10 20 305
float
5 float
6def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
8Python3
math.hypot[]
4
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
9 #include
1#include
2#include
3#include
4#include
5def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
0def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
8def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
0def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
6 def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
7Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.005
#include
7 #include
2#include
5Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.009
#include
7 #include
4#include
5Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.013
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.014
C#
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.00
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.016
>>> def test[x, y, z]: >>> print[x, y, z] >>> res = test[*[10, 20, 30]] 10 20 30 >>> res = test[**{'x': 1, 'y': 2, 'z': 3} ] 10 20 302
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.018
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.09
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
0>>> def test[x, y, z]: >>> print[x, y, z] >>> res = test[*[10, 20, 30]] 10 20 30 >>> res = test[**{'x': 1, 'y': 2, 'z': 3} ] 10 20 305
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.03
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.04
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.03
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.025
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.026
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.03
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.08
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
0Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.09
math.hypot[]
4
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.03
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.033
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.034
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.035
math.hypot[]
4
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
6 def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
7def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
0def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
8def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
0float
3 >>> def test[x, y, z]: >>> print[x, y, z] >>> res = test[*[10, 20, 30]] 10 20 30 >>> res = test[**{'x': 1, 'y': 2, 'z': 3} ] 10 20 305
float
5 Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.045
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
0Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.09
math.hypot[]
4
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
9 5.0 5.6222771187482394
math.hypot[]
4
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.052
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.053
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.054
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.055
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.056
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
0def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
8def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
8JavaScript
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.060
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.061
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.062
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
0Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.064
def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
0def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
6 def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
7def test[x, y]:
h = [x**2 + y**2]**0.5
return h
print[test[3,4]]
print[test[3.5,4.4]]
8Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.069
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.070
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.071
Input lengths of shorter triangle sides: a: 3 b: 4 The length of the hypotenuse is: 5.072
Độ phức tạp về thời gian: O [log [2*[s2]] trong đó s là mặt của hình chữ nhật. Vì độ phức tạp của thời gian của hàm SQRT sẵn có là o [log [n]]: O[log[2*[s2]] where s is the side of the rectangle. because time complexity of inbuilt sqrt function is O[log[n]]
Không gian phụ trợ: O [1] O[1]