题目大意:给一个T表示T组测试样例,接下来每行六个数表示两个圆的二维坐标x,y以及半径。问有多少条公共切线?如果有无数条输出-1

思路:将几种圆位置的情况利用两点间距离公式判断一下,防止精度问题直接判断距离的平方。

Code

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>

using namespace std;

int x1, yy1, r1;
int x2, yy2, r2;

//两点间距离的平方 
inline int calc_len (int x1, int y1, int x2, int y2) {
	return ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

int main() {
	int T;
	cin >> T;
	while (T--) {
		cin >> x1 >> yy1 >> r1;
		cin >> x2 >> yy2 >> r2;
		if (x1 == x2 && yy1 == yy2) {
			if (r1 == r2) {
				cout << -1 << endl;
			} else {
				cout << 0 << endl;
			}
			continue;
		}
		int db_len = calc_len(x1, yy1, x2, yy2);
		int db_rr1 = (r1 + r2) * (r1 + r2);
		int  db_rr2 = (r1 - r2) * (r1 - r2);
		if (db_len < db_rr2) {
			cout << 0 << endl;
		} else if (db_len == db_rr2) {
			cout << 1 << endl;
		} else if (db_len < db_rr1) {
			cout << 2 << endl;
		} else if (db_len == db_rr1) {
			cout << 3 << endl;
		} else {
			cout << 4 << endl;
		}
	} 
	return 0;
}