2 条题解

  • 0
    @ 2026-3-26 13:02:44

    NUMPY053 题解:\texttt{float32} 转 \texttt{int32}

    题目

    给定一个 \texttt{float32} 数组,将其转换为 \texttt{int32} 类型。

    解题思路

    1. 读取输入并指定 dtype 为 \texttt{float32}
    2. 使用 \texttt{astype} 转换为 \texttt{int32}
    3. 打印结果(会自动截断小数部分)

    代码

    import numpy as np
    
    n = int(input())
    arr = np.array(list(map(float, input().split())), dtype=np.float32)
    result = arr.astype(np.int32)
    print(result)
    

    代码详解

    • \texttt{dtype=np.float32} —— 指定数组数据类型
    • \texttt{astype} —— 类型转换方法
    • 小数转整数会直接截断

    运行示例

    输入:

    5
    10.5 20.3 30.7 40.1 50.9
    

    输出:

    [10 20 30 40 50]
    

    核心知识点

    1. \texttt{dtype=np.float32} —— 指定数据类型
    2. \texttt{astype} —— 类型转换
    3. \texttt{np.int32} —— 32 位整数类型
    • 0
      @ 2026-3-26 13:02:10

      NUMPY053 题解:\texttt{float32} 转 \texttt{int32}

      题目

      给定一个 \texttt{float32} 数组,将其转换为 \texttt{int32} 类型。

      解题思路

      1. 读取输入并指定 dtype 为 \texttt{float32}
      2. 使用 \texttt{astype} 转换为 \texttt{int32}
      3. 打印结果(会自动截断小数部分)

      代码

      import numpy as np
      
      n = int(input())
      arr = np.array(list(map(float, input().split())), dtype=np.float32)
      result = arr.astype(np.int32)
      print(result)
      

      代码详解

      • \texttt{dtype=np.float32} —— 指定数组数据类型
      • \texttt{astype} —— 类型转换方法
      • 小数转整数会直接截断

      运行示例

      输入:

      5
      10.5 20.3 30.7 40.1 50.9
      

      输出:

      [10 20 30 40 50]
      

      核心知识点

      1. \texttt{dtype=np.float32} —— 指定数据类型
      2. \texttt{astype} —— 类型转换
      3. \texttt{np.int32} —— 32 位整数类型
      • 1

      信息

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