2 条题解
-
0
NUMPY053 题解:\texttt{float32} 转 \texttt{int32}
题目
给定一个 \texttt{float32} 数组,将其转换为 \texttt{int32} 类型。
解题思路
- 读取输入并指定 dtype 为 \texttt{float32}
- 使用 \texttt{astype} 转换为 \texttt{int32}
- 打印结果(会自动截断小数部分)
代码
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]核心知识点
- \texttt{dtype=np.float32} —— 指定数据类型
- \texttt{astype} —— 类型转换
- \texttt{np.int32} —— 32 位整数类型
-
0
NUMPY053 题解:\texttt{float32} 转 \texttt{int32}
题目
给定一个 \texttt{float32} 数组,将其转换为 \texttt{int32} 类型。
解题思路
- 读取输入并指定 dtype 为 \texttt{float32}
- 使用 \texttt{astype} 转换为 \texttt{int32}
- 打印结果(会自动截断小数部分)
代码
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]核心知识点
- \texttt{dtype=np.float32} —— 指定数据类型
- \texttt{astype} —— 类型转换
- \texttt{np.int32} —— 32 位整数类型
- 1
信息
- ID
- 300
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- (无)
- 标签
- (无)
- 递交数
- 0
- 已通过
- 0
- 上传者