강승현입니다
article thumbnail
반응형

문제로이동


목차

     



    문제 설명

    알고리즘 문제라기 보다는 수학에 많이 가깝습니다.

    두 원을 그려서 교점(겹치는 점)을 찾아내는 문제이며, 정답률이 많이 낮은 것으로 봐서는 여러 경우의 수를 고려하지 못했던 것 같습니다.

    물론 저도 정답률 낮추는 데 한 몫 했습니다 ^^

     

    경우의 수는 크게 4가지

     

    소스코드도 위와 같은 순서로 분리되어 있습니다.

     

    1. 원이 완전히 겹치는 경우 (출력값 0)

    두 원의 거리가 0이면서 반지름도 같은 경우를 말합니다.

     

    2. 원이 한쪽 면만 닿는 경우 (출력값 1)

    (내접) 두 원의 거리 == 원1 반지름 + 원2 반지름

    단, 원1 반지름과 원2 반지름의 길이를 비교해서 작은 값, 큰 값으로 분류해도 되지만 편의상 절대값 abs를 이용했습니다.

    내접 두 원의 거리 == abs(r1 - r2)

    (외접) 두 원의 거리 - r1 = r2

    아래 그림을 참고 바랍니다.

     

    3. 원이 겹치고 교점이 2개 생기는 경우 (출력값 2)

    2가지 경우가 있는데 이 두 조건을 모두 만족하면 됩니다. 그림은 아래를 참고해 주세요!

     

    4. 원이 아예 겹치지 않는 경우 (출력값 0)

    2가지 경우가 있는데 이는 else로 분류했기 때문에 따로 조건 설정을 하지 않았습니다.


    소스 코드

    Python

    import sys,math
    input = sys.stdin.readline
    
    t = int(input())
    while t:
        t-=1
        x1,y1,r1,x2,y2,r2 = map(int,input().rstrip().split())
        x = x2-x1
        y = y2-y1
        d = math.sqrt(x**2+y**2) #두 원 거리
        if d==0 and r1==r2:ans=-1
        elif d==abs(r1-r2) or r1+r2==d:ans=1
        elif abs(r1-r2)<d<r1+r2:ans = 2
        else:ans=0
        print(ans)

    Java8

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.StringTokenizer;
    
    public class Main {	
    	public static void main(String[] args) throws IOException{
    		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    		StringTokenizer st = new StringTokenizer(bf.readLine());
    		int T= Integer.parseInt(st.nextToken());
    		for(int t = 0;t<T;t++) {
    			double x1,y1,r1,x2,y2,r2,d;
    			st = new StringTokenizer(bf.readLine());
    			x1 = Double.parseDouble(st.nextToken());
    			y1 = Double.parseDouble(st.nextToken());
    			r1 = Double.parseDouble(st.nextToken());
    			x2 = Double.parseDouble(st.nextToken());
    			y2 = Double.parseDouble(st.nextToken());
    			r2 = Double.parseDouble(st.nextToken());
    			double x = x2-x1;
    			double y = y2-y1;			
    			d =  Math.sqrt(x*x+y*y);
    			if(d==0 && r1==r2)//원이 완전히 겹치는 경우
    				System.out.println(-1);
    			else if(d==Math.abs(r1-r2) || r1+r2==d)//내접,외접			
    				System.out.println(1);
    			else if(Math.abs(r1-r2)<d && d<r1+r2)
    				System.out.println(2);
    			else
    				System.out.println(0);
    		}
    	}
    }
    
    반응형
    profile

    강승현입니다

    @CODe_

    포스팅에 대한 피드백을 환영합니다!
    피드백 작성자를 존중하며, 함께 공유하고 성장할 수 있는 기회가 되기를 기대합니다.의견이나 조언이 있다면 언제든지 자유롭게 남겨주세요!

    article prev thumbnail
    article next thumbnail
    profile on loading

    Loading...