当我打印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;
}
我收到了四舍五入的输出。
答案 0 :(得分:5)
您可以使用the member function设置std::cout
而不使用std::setprecision
的精度:
std::cout.precision(42);