Created
July 1, 2017 21:54
-
-
Save riemannulus/58783400862951012b69564d0a174c10 to your computer and use it in GitHub Desktop.
1002
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <math.h> | |
/* | |
이 문제는 중점 2개와 반지름 2개를 받아, 두 원의 교점의 갯수를 구하는 문제이다. | |
두 원의 교점을 구하기 위해서는, 두 원의 중점간의 거리를 구할 필요가 있다. | |
점간의 거리를 구하는 수식은 root(pow(x2 - x1, 2) + pow(y2 - y1, 2)) 이며, 값을 t라고 하자. | |
두 원의 반지름의 합을 d라고 할 때, 경우의 수를 살펴보면. | |
1. t > d | |
2. t == d | |
3. t < d | |
세 가지 경우로 나뉘는 것을 알 수 있다. | |
이 때 유의해야 할 점은, 두 원의 중점이 같을 경우이다. | |
1. 두 원의 중점이 같고, 반지름까지 같다면 동일한 원이므로 -1 | |
2. 두 원의 중점이 같고, 반지름이 다르다면 교점이 없으므로 0 | |
이 경우를 유의하면서 구현하면 된다. | |
큰 원 안에 작은 원이 들어갈 경우도 유의해야 하는데, | |
두 원의 반지름의 차의 절댓값이 t보다 큰 경우는 큰 원 안에 작은 원이 들어갔단 말이므로 0으로 놓으면 된다. | |
수행시간은 테스트케이스를 제외하고 O(1) 안에서 해결할 수 있다. | |
*/ | |
int main() | |
{ | |
int a, k, x; | |
double t, r1, r2; | |
int ar[6]; // x1 y1 r1 x2 y2 r2 | |
scanf("%d", &a); | |
while (a--) | |
{ | |
for (int i = 0; i < 6; i++) | |
{ | |
scanf("%d", &ar[i]); | |
} | |
t = sqrt(pow(ar[3] - ar[0], 2) + pow(ar[4] - ar[1], 2)); | |
k = ar[2] + ar[5]; | |
r1 = (ar[2] < ar[5]) ? ar[2] : ar[5]; //small one | |
r2 = (ar[2] > ar[5]) ? ar[2] : ar[5]; //big one | |
x = r1 + t; | |
if ((ar[0] == ar[3]) && (ar[1] == ar[4])) | |
{ | |
if(r1 == r2) printf("-1"); | |
else printf("0"); | |
} | |
else | |
{ | |
if ((t > k) || (t < abs(r1 - r2))) printf("0"); | |
else if ((t == k) || (t == abs(r1 - r2))) printf("1"); | |
else printf("2"); | |
} | |
printf("\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment