※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 25936번 문제인 Rain Gauge이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/25936
25936번: Rain Gauge
The first input line contains a positive integer, n, indicating the number of scenarios to check. Each of the following n lines contains a pair of integers, s, r (1 ≤ s ≤ 100, 1 ≤ r ≤ 100), which represents the length of the side of the skylight an
www.acmicpc.net
한 변의 길이가 s인 정사각형과 반지름의 길이가 r인 원을 두 도형의 중심이 일치하게 내려놓았을 때, 두 도형의 겹치는 부분의 넓이를 구하는 문제이다.
두 도형의 위치관계는 (1) 정사각형 내부에 원이 완전히 포함되거나 (2) 원 내부에 정사각형이 완전히 포함되거나 (3) 둘이 아니거나의 세 가지 경우가 존재한다. 이 세 가지로 경우를 나누어 문제를 해결하자.
(3)의 경우의 넓이를 구할 때, 부채꼴의 중심각의 크기는 cmath 헤더에 들어있는 역삼각함수(asin, acos, atan 등의 함수)를 이용해 구해낼 수 있다.
아래는 제출한 소스코드이다.
#define PI 3.14159265358979
#include <iostream>
#include <cmath>
using namespace std;
typedef long double ld;
void solve() {
int s, r; cin >> s >> r;
if (s * s < 2 * r * r) {
cout << s * s << ".00\n";
return;
}
else if (2 * r <= s) {
ld rr = r;
cout << PI * r * r << '\n';
}
else {
ld ss = s, rr = r;
ld h = sqrt(rr * rr - ss * ss / 4);
ld angle = 2 * PI - 8 * atan2(h , ss / 2);
cout << (2 * h * ss) + ((angle / 2) * rr * rr) << '\n';
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed;
cout.precision(2);
int T; cin >> T;
while (T--) solve();
}
'BOJ' 카테고리의 다른 글
[BOJ 25943 // C++] 양팔저울 (1) | 2023.03.15 |
---|---|
[BOJ 1071 // C++] 소트 (0) | 2023.03.14 |
[BOJ 5602 // C++] 問題1 (0) | 2023.03.13 |
[BOJ 16397 // C++] 탈출 (0) | 2023.03.12 |
[BOJ 27659 // C++] Queue skipping (Easy) (0) | 2023.03.11 |