E . Rain Gauge [ 问题 8629 ] [ 讨论 ]
Description
When going to your internship you got a nice apartment with a skylight. However, one crazy party later and you now have a square-shaped hole where your skylight used to be. Rather than telling the landlord, you decided you would “fix” it by putting a circular pot to collect the water but, as the saying goes, round peg square hole. You need to determine how much of the square the circle covers to help you determine if you should buy a larger pot. Don’t worry about the area not covered; you can do multiplication and subtraction easily in your head.
Given the radius of a circular pot and the length of the square skylight, calculate the amount of skylight rain area covered by the pot assuming the two shapes have the same center (i.e., are coaxial) with respect to the direction rain falls from (up). In other words, the center of the square will be directly above the center of the circle. See the picture for an example; let up be the direction from above the page, while the dotted square is the skylight and the solid circle is the pot to collect water.
Input
There is one input line, it contains a pair of integers, s,r (1≤s≤100, 1≤r≤100), which represents the length of the side of the skylight and the radius of the pot, respectively.
Output
Print a single decimal representing the area under the skylight that is covered by the pot. Round the answers to two decimal places (e.g., 1.234 rounds to 1.23 and 1.235 rounds to 1.24). For this problem, use 3.14159265358979 as the value of pi.
Samples
Input 复制
1 1
Output
1.00
Input 复制
8 5
Output
62.19
Input 复制
10 4
Output
50.27
Source
UCF 2021 Practice
分成三种情况
1 圆在正方形内
2 正方形在圆内
3 图示
#include<bits/stdc++.h>
using namespace std;
#define pi 3.14159265358979
int main()
{
double s,r,A;
cin>>s>>r;A=acos(s/(2*r));
if(r*r>=s*s/2)
printf("%.2lf",s*s);
else if(s/2>=r)
printf("%.2lf",pi*r*r);
else
{
double s1,s2;
//s1=2*s*sqrt(r*r-s*s/4);
s1=2*r*r*sin(2*A);
s2=r*r*(2*pi-8*A)/2;
printf("%.2lf",s1+s2);
}
}