1 条题解

  • 0
    @ 2026-3-26 15:56:55

    NUMPY091 题解:计算向量范数

    题目

    计算向量的欧几里得范数(L2范数)。

    解题思路

    1. 读取数组
    2. 使用 np.linalg.norm\texttt{np.linalg.norm} 计算范数

    代码

    import numpy as np
    
    n = int(input())
    arr = np.array(list(map(float, input().split())))
    result = np.linalg.norm(arr)
    print(result)
    

    代码详解

    欧几里得范数(L2范数):

    $$\|x\|_2 = \sqrt{x_1^2 + x_2^2 + \cdots + x_n^2}$ ## 运行示例 **输入:** ``` 3 3.0 4.0 0.0 ``` **分析:** - $\|x\|_2 = \sqrt{3^2 + 4^2 + 0^2} = \sqrt{9 + 16 + 0} = \sqrt{25} = 5$ **输出:** ``` 5.0 ``` ## 核心知识点 1. $\texttt{np.linalg.norm}$ —— 计算范数$$
    • 1

    信息

    ID
    408
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    (无)
    递交数
    0
    已通过
    0
    上传者