如何在没有iomanip的情况下在没有头文件的情况下设置精度

时间:2019-07-23 17:02:56

标签: c++ codeblocks

当我打印max1的值时,我会得到四舍五入的输出。

使用setprecision确实解决了我的问题,但是我想解决该问题而不使用iomanip之类的其他头文件。

int main()
{
    long long int n, l;

    cin >> n >> l;
    long long int a[n];

    for(int i = 0; i < n; i++)
         cin >> a[i];

    sort(a, a + n);

    double max1 = 0;
    max1 = 2*mmax(a[0] - 0, l - a[n - 1]);
    for(int i = 0; i < n - 1; i++)
     {
          max1 = mmax(max1,(a[i + 1] - a[i]));
     }
    cout << max1/2.0;
    //cout << fixed << setprecision(6) << max1/2.0;
}

我收到了四舍五入的输出。

1 个答案:

答案 0 :(得分:5)

您可以使用the member function设置std::cout而不使用std::setprecision的精度:

 std::cout.precision(42);