1 条题解

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

    NUMPY098 题解:计算直方图

    题目

    计算数据的直方图。

    解题思路

    1. 读取数据和箱数
    2. 使用 np.histogram\texttt{np.histogram} 计算直方图

    代码

    import numpy as np
    
    n, bins = map(int, input().split())
    arr = np.array(list(map(float, input().split())))
    hist, _ = np.histogram(arr, bins=bins)
    print(hist)
    

    运行示例

    输入:

    10 5
    0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
    

    分析:

    • 数据范围 [0,10)[0, 10),分成 5 个箱
    • 每个箱包含 2 个数据

    输出:

    [2 2 2 2 2]
    

    核心知识点

    1. np.histogram\texttt{np.histogram} —— 计算直方图
    • 1

    信息

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