FZU 2213 Common Tangents(公切线)


Description


题目描述


Two different circles can have at most four common tangents.

The picture below is an illustration of two circles with four common tangents.

Now given the center and radius of two circles, your job is to find how many common tangents between them.


两个不同的圆最多可以有4种公切线。

下图表示两个圆的4种公切线。

现在分别给你两个圆的圆心和半径,求他们之间有几条公切线。


Input


输入


The first line contains an integer T, meaning the number of the cases (1 <= T <= 50.).

For each test case, there is one line contains six integers x1 (−100 ≤ x1 ≤ 100), y1 (−100 ≤ y1 ≤ 100), r1 (0 < r1 ≤ 200), x2 (−100 ≤ x2 ≤ 100), y2 (−100 ≤ y2 ≤ 100), r2 (0 < r2 ≤ 200).

Here (x1, y1) and (x2, y2) are the coordinates of the center of the first circle and second circle respectively, r1 is the radius of the first circle and r2 is the radius of the second circle.


输入的首行是一个整数T,表示测试样例的数量。(1 <= T <= 50)。

每个测试样例都是一行6个整数,x1 (−100 ≤ x1 ≤ 100), y1 (−100 ≤ y1 ≤ 100), r1 (0 < r1 ≤ 200), x2 (−100 ≤ x2 ≤ 100), y2 (−100 ≤ y2 ≤ 100), r2 (0 < r2 ≤ 200)。

其中(x1, y1)与(x2, y2)分别是第一个圆与第二个圆的圆心,r1、r2分别是第一个圆与第二个圆的半径。


Output


输出


For each test case, output the corresponding answer in one line.

If there is infinite number of tangents between the two circles then output -1.


每个测试样例的结果输出一行。

如果存在无数条公切线则输出-1。


Sample Input - 输入样例


Sample Output - 输出样例


3

10 10 5 20 20 5

10 10 10 20 20 10

10 10 5 20 10 5


4

2

3

【题解】

根据圆的位置关系分情况即可。

1. 圆心距>半径和,相离,4条公切线。

2. 圆心距==半径和,外切,3条公切线。

3. 半径差<圆心距<半径和,相交,2条公切线。

4. 圆心距==半径差,圆心不等,内切,1条公切线;圆心相等,重合,无数条公切线。

5. 圆心距<半径差,内含,0条公切线。

接着用平方表示就能解决误差的问题,直接可用int进行比较。

【代码 C++】

 1 #include<cstdio>
 2 #include<cstring>
 3 int main(){
 4     int t, x1, y1, r1, x2, y2, r2, c_distance, r_sum, r_difference;
 5     scanf("%d", &t);
 6     while (t--){
 7         scanf("%d%d%d%d%d%d", &x1, &y1, &r1, &x2, &y2, &r2);
 8         c_distance = (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2);
 9         r_sum = (r1 + r2)*(r1 + r2);
10         r_difference = r_sum - (r1*r2 << 2);
11         if (c_distance > r_sum) puts("4");//相离
12         else if (c_distance == r_sum) puts("3");//外切
13         else if (r_difference < c_distance &&c_distance < r_sum) puts("2");//相交
14         else if (c_distance == r_difference){
15             if (x1 ^ y1 ^ x2 ^ y2) puts("1");//内切
16             else puts("-1");//重合
17         }
18         else puts("0");//内含
19     }
20     return 0;
21 }
时间: 2024-10-05 08:37:29

FZU 2213 Common Tangents(公切线)的相关文章

第六届福建省大学生程序设计竞赛不完全题解

昨天自己队做了一下第六届福建省赛,感觉有些蒙,赛后补了几道题,能力有限,一共只出A了7道题 A题   Super Mobile Charger 题目链接 http://acm.fzu.edu.cn/problem.php?pid=2212 水题 #include<iostream> #include<cstdio> #include<algorithm> using namespace std; int a[10005]; int b[10005]; int main(

UVA - 10674-Tangents

 题意:给出两个圆,求它们的公切线,并按照一定格式输出 做法:模拟 代码: #include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include&

2015福建省赛

 Problem A Super Mobile Charger Accept: 217    Submit: 402 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description While HIT ACM Group finished their contest in Shanghai and is heading back Harbin, their train was delayed due to the hea

UVa 10674 (求两圆公切线) Tangents

题意: 给出两个圆的圆心坐标和半径,求这两个圆的公切线切点的坐标及对应线段长度.若两圆重合,有无数条公切线则输出-1. 输出是按照一定顺序输出的. 分析: 首先情况比较多,要一一判断,不要漏掉. 如果高中的那点老底还在的话,代码还是很好理解的. 1 //#define LOCAL 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #incl

Longest Common Subsequence

Problem statement: Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Have you met this question in a real interview? Yes Clarification What's the definition of Longest Common Subsequence? https:/

ANSI Common Lisp Chapter 2

Chapter 2 总结 (Summary) Lisp 是一种交互式语言.如果你在顶层输入一个表达式, Lisp 会显示它的值. Lisp 程序由表达式组成.表达式可以是原子,或一个由操作符跟着零个或多个实参的列表.前序表示法代表操作符可以有任意数量的实参. Common Lisp 函数调用的求值规则: 依序对实参从左至右求值,接着把它们的值传入由操作符表示的函数. quote 操作符有自己的求值规则,它完封不动地返回实参. 除了一般的数据类型, Lisp 还有符号跟列表.由于 Lisp 程序是

14. Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. 这个问题是关于如何寻找字符串数组的公共前缀的,从第一个字母开始,只要共有的都要输出. 思路: 1.首先,如何得到字符串共有前缀,既然共有,那么很容易先想到最短字符串. 2.最短字符串和其余进行比较,这样就省下了大于最短字符串长度的比较. 3.关于字符串的操作,length和null是很必要的判断输入操作,千万别忘记! 4.关

FZU 2150 Fire Game(点火游戏)

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-size: 10.5000pt } h2 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 18.0000pt } h3 {

235. Lowest Common Ancestor of a Binary Search Tree

1. 问题描述 Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T th