본문 바로가기
코테준비/하루한개도전~

백준 : 1002 - 터렛

by 움바둠바 2024. 7. 1.
728x90

https://www.acmicpc.net/problem/1002

ㅋㅋ.. 여러번의 시도끝에 해결했다!

9달전에 하다가 빢쳐서 그만뒀나보다ㅎㅎ


풀이는 간단하다!

두 원이 주어졌다고 생각하고, 원의 접점을 생각하는 문제이다.

 

나는 반례하나를 생각하지 못해서 계속 틀렸다..ㅋ

#include <iostream>
#include <cmath>
using namespace std;

double get_distance(int x1, int y1, int x2, int y2) {
	int x = x1 - x2;
	int y = y1 - y2;

	return sqrt((x*x) + (y*y));

}

int comdouble(double x, double y, double absTolerance = (1.0e-8))
{
	double diff = x - y;
	if (fabs(diff) <= absTolerance)
		return 0;

	return (diff > 0) ? 1 : -1;
}


int main() {

	int testcase;
	int* answer;
	

	cin >> testcase;

	answer = new int[testcase];

	for (int i = 0; i < testcase; i++) {
		int x1, y1, r1, x2, y2, r2;
		cin >> x1 >> y1 >> r1 >> x2 >> y2 >> r2;
		double distance = get_distance(x1, y1, x2, y2);

		if (distance == 0 ) {
			if ((r1 == r2)) {
				answer[i] = -1;
				continue;
			}
			answer[i] = 0;
			continue;
		}

		double r = r1 + r2;
		

		if (r < distance) {
			answer[i] = 0;
			continue;
		}
		else if (r > distance) {
			/*
			if ( comdouble((distance + (double)r1), r2) || comdouble((distance + (double)r2), r1)) {
				answer[i] = 1;
				continue;
			}
			*/
			if (((distance + (double)r1) == r2) || ((distance + (double)r2) == r1)) {
				answer[i] = 1;
				continue;
			}
			else if (((distance + (double)r1) < r2) || ((distance + (double)r2) < r1)) {
				answer[i] = 0;
				continue;
			}
			
			answer[i] = 2;
			continue;
		}
		else {
			answer[i] = 1;
			continue;
		}

		answer[i] = 2;
		

	}

	
	for (int i = 0; i < testcase; i++) {
		cout << answer[i] << endl;
	}
	



	return 0;
}

Comdouble

61번째 줄에서, double의 경우 오차때문에 == 가 아니라, 입실론을 이용해 비교를 해줘야 한다고 생각했다.

근데?? 이렇게 하면 오히려 테스트케이스를 통과하지 못하고,  ==를 사용해야 통과하였다.

왜지...?


매일 한문제 이상 푸는게 목표이다!

깃허브에 잔디도 심으려고 백준허브도 연동했다ㅎㅎㅎ

방학동안 열심히 해야지

728x90