From 74402612180e4ea792c456254e95f54fa19251e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BD=B3=E8=B1=AA?= <2509165033@student.example.com> Date: Tue, 21 Apr 2026 11:25:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ljh.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 ljh.py diff --git a/ljh.py b/ljh.py new file mode 100644 index 0000000..d0c4028 --- /dev/null +++ b/ljh.py @@ -0,0 +1,67 @@ + +print("题目1 ") + +hello1 = "Hello" +hello2 = 'Hello' +print("双引号表示:", hello1) +print("单引号表示:", hello2) + + +print("\n1. 'Hello' 每个字符的ASCII码:") +for c in hello1: + print(f"字符 '{c}' → ASCII: {ord(c)}") + + +print("\n2. 验证ASCII码65对应的字符:") +print(f"chr(65) → {chr(65)}") + + + +import math + + +print("\n题目3") +A = [3, 4] +B = [1, 2] + + +A_plus_B = [A[0] + B[0], A[1] + B[1]] +print(f"1. A + B = {A_plus_B}") + + +two_times_A = [2 * A[0], 2 * A[1]] +print(f"2. 2 × A = {two_times_A}") + + +len_A = math.sqrt(A[0] ** 2 + A[1] ** 2) +print(f"3. 向量A的长度 = {len_A}") + + + +print("\n题目4") +A3 = [1, 2, 3] +B3 = [4, 5, 6] + + +dot_product = A3[0]*B3[0] + A3[1]*B3[1] + A3[2]*B3[2] +print(f"1. 点积A·B = {dot_product}") + + +def cosine_similarity(a, b): + + dot = sum(x*y for x,y in zip(a,b)) + + norm_a = math.sqrt(sum(x*x for x in a)) + norm_b = math.sqrt(sum(x*x for x in b)) + + return dot / (norm_a * norm_b) + +cos_sim = cosine_similarity(A3, B3) +print(f"2. 余弦相似度 = {cos_sim:.4f}") + + +A_test = [1, 0] +B_test = [0, 1] +cos_sim_test = cosine_similarity(A_test, B_test) +print(f"3. A=[1,0], B=[0,1]的余弦相似度 = {cos_sim_test}") +print("原因:两个向量正交(垂直),方向完全不同,所以余弦相似度为0") \ No newline at end of file