두 원의 중심과 반지름만 있으면 관계가 어떻게 되는지 구할 수 있다.
문제를 풀면서 distance를 실수형으로 안해서 많이 틀렸다.
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 <cstdio> | |
#include <math.h> | |
/** | |
* https://www.acmicpc.net/problem/1002 | |
* BOJ 백준온라인져지 1002 터렛 풀이 | |
*/ | |
int max(float a, float b){ | |
return a>b?a:b; | |
} | |
int min(float a, float b){ | |
return a>b?b:a; | |
} | |
int main(){ | |
int N; | |
scanf("%d",&N); | |
while(N--){ | |
int x1, y1, r1, x2, y2, r2; | |
scanf("%d%d%d%d%d%d",&x1,&y1,&r1,&x2,&y2,&r2); | |
double d = sqrt(pow((x1-x2), 2) + pow(y1-y2, 2)); // distance = (x1 - x2)^2 + (y1 - y2)^2 | |
int result = 0; | |
if(d == 0){ | |
if(r1 == r2) | |
result = -1; // 겹치는 경우 | |
}else if(r1 + r2 == d){ | |
result = 1; // 한 점에서 만나는 경우(외접) | |
}else if(r1 + r2 > d){ // 원 안의 원 | |
if(d + min(r1, r2) == max(r1, r2)) | |
result = 1; // 내접 | |
else if(d > max(r1, r2) - min(r1, r2)) | |
result = 2; | |
} | |
printf("%d\n",result); | |
} | |
return 0; | |
} |
예제 입력
3 0 0 13 40 0 37 0 0 3 0 7 4 1 1 1 1 1 5
예제 출력
2 1 0
'IT > 알고리즘' 카테고리의 다른 글
BOJ 백준온라인져지 11651 좌표 정렬하기 2 풀이 (0) | 2017.11.30 |
---|---|
BOJ 백준온라인져지 1004 어린 왕자 풀이 (0) | 2017.11.30 |
BOJ 백준온라인져지 11866 조세퍼스 문제 0 풀이 (0) | 2017.11.29 |
BOJ 백준온라인져지 10845 큐 풀이 (0) | 2017.11.29 |
BOJ 백준온라인져지 2504 괄호의 값 풀이 (0) | 2017.11.28 |