poj 2242 The Circumference of the Circle

/* Name:2242 The Circumference of the Circle Author:Unimen Date: 18/04/11 18:24 */ /* 解题报告: 1、r = abc/4s 2、求面积有两种解法:海伦公式,三角形有向面积 3、C++里输出小数的用法 4、小数为负数的判断要注意不要与0比较,见注释 */ //海伦公式解法 #include <iostream> #include <cmath> #include <iomanip> using namespace std; const double PI = 3.141592653589793; double nX1, nX2, nX3, nY1, nY2, nY3; double square(double x) { return x*x; } int main() { while(cin>>nX1>>nY1>>nX2>>nY2>>nX3>>nY3) { //double s = nX1*nY2 + nX3*nY1 +nX2*nY3 - nX3*nY2 - nX2*nY1 - nX1*nY3; double a = sqrt(square(nX1 - nX2) + square(nY1 - nY2)); double b = sqrt(square(nX1 - nX3) + square(nY1 - nY3)); double c = sqrt(square(nX2 - nX3) + square(nY2 - nY3)); double p = (a + b + c) / 2; double s = sqrt(p * (p - a) * (p - b) * (p - c)); double r = a * b * c / (s * 4); cout<<fixed<<setprecision(2)<<2 * PI * r<<endl; } return 0; } //有向面积解法 #include <iostream> #include <iomanip> #include <cmath> using namespace std; const double PI = 3.141592653589793; double nX1, nX2, nX3, nY1, nY2, nY3; double square(double x) { return x*x; } int main() { while(cin>>nX1>>nY1>>nX2>>nY2>>nX3>>nY3) { double s = (nX1*nY2 + nX3*nY1 +nX2*nY3 - nX3*nY2 - nX2*nY1 - nX1*nY3)/2; if(s < 1e-9) //判断小数为负数用这种方法 { s = -s; } double a = sqrt(square(nX1 - nX2) + square(nY1 - nY2)); double b = sqrt(square(nX1 - nX3) + square(nY1 - nY3)); double c = sqrt(square(nX2 - nX3) + square(nY2 - nY3)); double r = a * b * c / (4 * s); cout<<fixed<<setprecision(2)<<2 * PI * r<<endl; } return 0; }

你可能感兴趣的:(c,Date)