hdu 2001 保留位

按有效位输出是 setprecision,按小数位数输出也是setprecision,但到底是谁取决于fixed。

const double value = 12.3456789

cout << setprecision(4) << value << endl; // 改成4精度,所以输出为12.35

cout << fixed << setprecision(4) << value << endl; // 加了fixed意味着是固定点方式显示,所以这里的精度指的是小数位,输出为12.3457

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main(){
	double x1,y1,x2,y2,d;
	while(cin>>x1>>y1>>x2>>y2){
		d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
		cout<<fixed<<setprecision(2)<<d<<endl;
	}return 0;
}


你可能感兴趣的:(hdu 2001 保留位)