fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip> // For std::fixed and std::setprecision
  4.  
  5. // Define M_PI if not already defined (common in some compilers)
  6. #ifndef M_PI
  7. #define M_PI 3.14159265358979323846
  8. #endif
  9.  
  10. // Function to calculate the grazing area
  11. double calculateGrazingArea(double R, double r) {
  12. // This formula applies when the goat is tied to the edge of a circular field,
  13. // and the rope length 'r' allows the goat to graze within the field.
  14. // The problem often involves finding 'r' such that the grazing area is half the field area.
  15.  
  16. // If the rope is too short to reach the field's center, the grazing area is simply pi * r^2.
  17. if (r <= R) {
  18. return M_PI * r * r / 2.0; // Half circle if tied to edge
  19. } else {
  20. // More complex calculation for when the rope extends beyond the center
  21. // This is a simplified representation, the actual formula is more involved
  22. // and often requires solving a transcendental equation for the angle.
  23. // For a more accurate solution, numerical methods are usually needed.
  24. // This example assumes a scenario where the rope is long enough to cover a significant portion.
  25. double alpha = 2 * acos(R / r); // Angle subtended by the field's radius at the goat's tether point
  26. double area_segment = r * r * alpha - R * sqrt(r * r - R * R);
  27. return M_PI * R * R - area_segment; // Placeholder, actual formula is more specific
  28. }
  29. }
  30.  
  31. int main() {
  32. double fieldRadius, ropeLength;
  33.  
  34. std::cout << "Enter the radius of the circular field (R): ";
  35. std::cin >> fieldRadius;
  36.  
  37. std::cout << "Enter the length of the goat's rope (r): ";
  38. std::cin >> ropeLength;
  39.  
  40. // In this specific common variation, the third input (theta) is usually derived or part of a more complex scenario.
  41. // For simplicity in this example, we are not directly taking 'theta' as an input for the core calculation,
  42. // as it's often a result of the geometry of R and r.
  43. // If a specific 'theta' is required for a different problem variation, it would be added here.
  44.  
  45. double grazingArea = calculateGrazingArea(fieldRadius, ropeLength);
  46.  
  47. std::cout << std::fixed << std::setprecision(4); // Format output to 4 decimal places
  48. std::cout << "The grazing area is: " << grazingArea << std::endl;
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0.01s 5332KB
stdin
0.69
0.87
stdout
Enter the radius of the circular field (R): Enter the length of the goat's rope (r): The grazing area is: 0.8699