백준 1002번 터렛

Develop My Life·2022년 3월 6일
0

PS

목록 보기
13/32

문제 링크

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

문제

풀이

//시작 15:50
//끝 17:16

#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

int main() {
	vector<int> ans;
	int x1, y1, r1, x2, y2, r2;
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> x1 >> y1 >> r1 >> x2 >> y2 >> r2;
		//l은 중심 사이의 거리로 double로 선언해야 맞다.
		//int로 선언하는 경우 소수자리가 버려져 제대로된 비교가 불가능하기 때문
		double l = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
		if (r1 == r2 && l == 0) {
			ans.push_back(-1);
		}
		else if (r1 + r2 == l || abs(r1 - r2) == l) {
			ans.push_back(1);
		}
		else if (abs(r1 - r2) < l && l < r1 + r2) {
			ans.push_back(2);
		}
		else {
			ans.push_back(0);
		}

		
		
		
	}
	for (int i = 0; i < ans.size(); i++) {
		cout << ans[i] << '\n';
	}


	return 0;
}
  • 보기에는 쉬워보였지만 이 문제에서 계속 틀렸습니다가 뜬 이유는 두 원의 중심 사이의 거리를 int 형으로 선언했기 때문이다. 거리는 double 형으로 선언해야 소수점까지 유지하여 올바른 비교를 할 수 있어 맞출 수 있다.

0개의 댓글