以下是 C++ 代码实现:

#include <iostream>
using namespace std;

int main() {
    double a = 3.0/2.0;
    int b = a; // 将 double 转换为 int,自动截断小数部分
    cout << b << endl; // 输出 2
    return 0;
}

这段代码首先计算出 3.0/2.0 的值,即 1.5,然后将其转换为 int 类型变量 b,这时小数部分被截断了,b 的值变为 2。最后输出 b 的值即可。

C++ 代码实现 3/2 等于 2 的方法

原文地址: https://www.cveoy.top/t/topic/lB0D 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录