计算两点间的距离问题

#include <iostream>
#include<math.h> #include<iomanip> using namespace std; int main() { float i=0,a,b,c,d; float m; for(i=0;cin>>a>>b>>c>>d;i++) { m=sqrt((a-c)*(a-c)+(b-d)*(b-d)); //printf("%.2f\n",m); cout<<setprecision(2)<<fixed<<(sqrt((pow(a-c,2))+(pow(b-d,2))))<<endl; } } 
 第一:注意输入的数据要是浮点型,这一点比较容易忽视
第二:c++里面setprecision可以实现对输出数字的位数控制,但不会输出小数末尾的零,而且存在四舍五入,头文件为#include
第三:fixed和setprecision结合的实现控制小数点后位数的输出

猜你喜欢

转载自www.cnblogs.com/ZJK132/p/12283609.html
今日推荐