Algorithm
problem Link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1619
A cow is grazing in the field. A rope in the field is tied with two pillars. The cow is kept tied with the rope with the help of a ring. So the cow can be considered to be tied with any point of the rope. Your job is to find the area of the field where the cow can reach and eat grass. If required assume that π = 2 ∗ cos−1 (0) (Here angle is measured in radians). You can also assume that the thickness of the rope is zero, the cow is a point object and the radius of the ring and the thickness of the pillars are negligible. Please use double precision floating-point data type for floating-point calculations. Input First line of the input file contains an integer (N ≤ 100), which indicates how many sets of inputs are there. Each of the next N lines contains two integers D (0 ≤ D ≤ 1000) and L (D < L ≤ 1500). The first integer D denotes the distance in feet between the two pillars and the second integer L denotes the length of the rope in feet. Output Your program should produce N lines of output. Each line contains a single floating-point number, which has three digits after the decimal point. This floating-point number indicates the area of the field which the cow can reach and eat grass.
Sample Input 3 10 12 23 45 12 18
Sample Output 62.517 1366.999 189.670
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <bits/stdc++.h>
using namespace std;
#define PI 2*acos(0)
struct point {
double x, y;
point() { x = y = 0.0; }
point(double _x, double _y) : x(_x), y(_y) {}
};
// area of ellipse = a*b*pi, where a and b are half of minor and major axes
int main() {
int n;
double d,l;
scanf("%d",&n);
while(n--) {
scanf("%lf %lf",&d,&l);
double a = l/2;
double b = sqrt(l/2*l/2-d/2*d/2);
double area = PI*a*b;
printf("%.3f\n",area);
}
}
Copy The Code &
Try With Live Editor
Input
10 12
23 45
12 18
Output
1366.999
189.670
Demonstration
UVA Online Judge solution - 10678-The Grazing Cow - UVA Online Judge solution in C,C++,java