Translate

Views

Tuesday, December 12, 2023

Solution UVA: 438 The Circumference of the Circle


  Problem  VerdictLangTimeBestRankSubmit Time
 | discuss438 - The Circumference of the Circle AcceptedC++110.0000.00026623 mins ago

 

#include <bits/stdc++.h>
using namespace std;

double sqr(double x){
    return x*x;
}

double sqrt(double x){
    return pow(x, 1.0/2);
}

int main() {
    double xa, ya, xb, yb, xc, yc;
   
    while(cin >> xa >> ya >> xb >> yb >> xc >> yc){
        double ab = sqrt(sqr(xb-xa)+sqr(yb-ya));
        double bc = sqrt(sqr(xc-xb)+sqr(yc-yb));
        double ac = sqrt(sqr(xc-xa)+sqr(yc-ya));
        double p = (ab + bc + ac) / 2;
        double s = sqrt(p*(p-ab)*(p-bc)*(p-ac));
        double r = ab*bc*ac / (4*s);
        cout<<fixed<<setprecision(2)<<3.141592653589793*2*r<<"\n";
    }
   
}


  1. Function Definitions:

    • double sqr(double x): Defines a function to compute the square of a given double value.
    • double sqrt(double x): Defines a function to compute the square root of a given double value using the pow function.
  2. Main Function:

    • Declares six variables xa, ya, xb, yb, xc, yc to store the coordinates of three points.
  3. While Loop:

    • Uses a while loop that reads input for the coordinates of three points until it reaches the end of the input.
    • Inside the loop, it calculates the distances ab, bc, and ac between the points using the distance formula.
  4. Circumference Calculation:

    • Calculates the semi-perimeter p using the formula: =++2.
    • Calculates the area s of the triangle using Heron's formula: =()()().
    • Computes the radius r of the circumcircle using the formula: =4.
    • Finally, calculates the circumference of the circumcircle and outputs it with fixed precision.

No comments: