完成作业
This commit is contained in:
11
25 2.py
Normal file
11
25 2.py
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
A = [3, 4]
|
||||||
|
B = [1, 2]
|
||||||
|
|
||||||
|
A_plus_B = [A[0] + B[0], A[1] + B[1]]
|
||||||
|
print("A + B =", A_plus_B)
|
||||||
|
|
||||||
|
two_A = [2 * A[0], 2 * A[1]]
|
||||||
|
print("2 × A =", two_A)
|
||||||
|
|
||||||
|
norm_A = (A[0]**2 + A[1]**2)**0.5
|
||||||
|
print("A的模 =", norm_A)
|
||||||
21
25 3.py
Normal file
21
25 3.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import math
|
||||||
|
|
||||||
|
A = [1, 2, 3]
|
||||||
|
B = [4, 5, 6]
|
||||||
|
|
||||||
|
dot_product = A[0]*B[0] + A[1]*B[1] + A[2]*B[2]
|
||||||
|
print("A·B =", dot_product)
|
||||||
|
|
||||||
|
norm_A = math.sqrt(A[0]**2 + A[1]**2 + A[2]**2)
|
||||||
|
norm_B = math.sqrt(B[0]**2 + B[1]**2 + B[2]**2)
|
||||||
|
cos_sim = dot_product / (norm_A * norm_B)
|
||||||
|
print("余弦相似度 =", cos_sim)
|
||||||
|
|
||||||
|
A2 = [1, 0]
|
||||||
|
B2 = [0, 1]
|
||||||
|
dot_product2 = A2[0]*B2[0] + A2[1]*B2[1]
|
||||||
|
norm_A2 = math.sqrt(A2[0]**2 + A2[1]**2)
|
||||||
|
norm_B2 = math.sqrt(B2[0]**2 + B2[1]**2)
|
||||||
|
cos_sim2 = dot_product2 / (norm_A2 * norm_B2)
|
||||||
|
print("A=[1,0]与B=[0,1]的余弦相似度 =", cos_sim2)
|
||||||
|
print("原因:两个向量正交(垂直),方向完全不同,点积为0,所以余弦相似度为0")
|
||||||
7
2509165025.py
Normal file
7
2509165025.py
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
s = "Hello"
|
||||||
|
print("Hello 每个字符的ASCII码:")
|
||||||
|
for char in s:
|
||||||
|
print(f"{char}: {ord(char)}")
|
||||||
|
|
||||||
|
print("\n验证65对应的字符:")
|
||||||
|
print(f"chr(65) = {chr(65)}")
|
||||||
Reference in New Issue
Block a user