ps연습장
백준 7869번 : 두원 (C/C++)
hwsyl
2023. 5. 16. 14:54
반응형
<문제>
백준 7868번 두원
<풀이>
3가지 경우가 있다.
- 한원이 한원을 포함하는 경우
- 안겹치는 경우
- 일부분이 겹치는 경우
1의 경우에는 작은원의 넓이를 출력하고, 2의 경우는 0을 출력한다.
3의 경우는 코사인 공식을 이용하여 세타 값을 구해서 넓이를 구해주면 된다.
<코드>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
#include<bits/stdc++.h>
#define fio() \
ios_base::sync_with_stdio(0); \
cin.tie(0)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tpi;
typedef tuple<ll, ll, ll> tpl;
typedef pair<double, ll> pdl;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };
const double pi = acos(-1);
struct pos{
ll x, y;
};
pos p[1010];
ll ccw(pos p1, pos p2, pos p3){
return p1.x*p2.y + p2.x*p3.y+p3.x*p1.y - (p2.x*p1.y+p3.x*p2.y+p1.x*p3.y);
}
int main(){
double x1, y1, r1, x2, y2, r2;
scanf("%lf %lf %lf %lf %lf %lf", &x1, &y1, &r1, &x2, &y2, &r2);
if(r1 > r2){
swap(x1, x2); swap(y1, y2); swap(r1, r2);
}
double dd = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
double aa = (r2-r1)*(r2-r1);
double bb = (r2+r1)*(r2+r1);
if(aa < dd && dd < bb){
double cosA = (r1*r1+dd-r2*r2)/(2.0*r1*sqrt(dd));
double cosB = (r2*r2+dd-r1*r1)/(2.0*r2*sqrt(dd));
double theta1 = acos(cosA);
double theta2 = acos(cosB);
double area1 = r1*r1*(theta1 - sin(theta1)*cosA);
double area2 = r2*r2*(theta2 - sin(theta2)*cosB);
printf("%.3lf", area1+area2);
}
else if(dd <= aa){
printf("%.3lf", pi*r1*r1);
}
else{
printf("%.3f", 0);
}
}
|
cs |
<회고>
코사인 법칙을 적용해볼 수 있었다.