1 条题解

  • 0
    @ 2026-3-26 15:57:22

    NUMPY096 题解:计算分位数

    题目

    计算数组的指定分位数。

    解题思路

    1. 读取数组和分位数
    2. 使用 np.percentile\texttt{np.percentile} 计算

    代码

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

    代码详解

    np.percentile(arr, q)\texttt{np.percentile(arr, q)} —— 计算数组 arrarrqq 分位数。

    运行示例

    输入:

    9 25
    1 2 3 4 5 6 7 8 9
    

    分析:

    • 25 分位数:将数据分成 4 份,25% 的数据小于等于这个值
    • 对于 [1,2,3,4,5,6,7,8,9][1,2,3,4,5,6,7,8,9],25 分位数是 33

    输出:

    3.0
    

    核心知识点

    1. np.percentile\texttt{np.percentile} —— 计算分位数
    • 1

    信息

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