본문 바로가기

알고리즘/BOJ

[Python&C++]1002-터렛

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

 

1002번: 터렛

각 테스트 케이스마다 류재명이 있을 수 있는 위치의 수를 출력한다. 만약 류재명이 있을 수 있는 위치의 개수가 무한대일 경우에는 -1을 출력한다.

www.acmicpc.net

(x1, y1)이 중심이고, 반지름이 r1인 원과, (x2, y2)이 중심이고, 반지름이 r2인 원의 교점 개수 구하는 문제이다.

Python 

import math
for _ in range(int(input())):
    x1, y1, r1, x2, y2, r2 = [int(i) for i in input().split()]
    circleDistance = math.sqrt((x1-x2)**2 + (y1-y2)**2)
    if x1 == x2 and y1 == y2 and r1 == r2:
        print(-1)
    elif circleDistance < abs(r1-r2) or circleDistance > r1 + r2:
        print(0)
    elif circleDistance == abs(r1-r2) or circleDistance == r1+r2:
        print(1)
    else:
        print(2)

C++

#include <iostream>
#include <cmath>

int main()
{
    int loopNum, x1, x2, y1, y2, r1, r2;
    double circleDistance;
    std::cin >> loopNum;
    for(int i = 0; i < loopNum; i++)
    {
        std::cin >> x1 >> y1 >> r1 >> x2 >> y2 >> r2;
        circleDistance = sqrt(pow((x1-x2), 2) + pow((y1-y2), 2));
        if(x1 == x2 && y1 == y2 && r1 == r2) 
        {
            std::cout << "-1\n";
        }
        else if(circleDistance < abs(r1 - r2) || circleDistance > r1 + r2)
        {
            std::cout << "0\n";
        }
        else if(circleDistance == abs(r1 - r2) || circleDistance == r1 + r2)
        {
            std::cout << "1\n";
        }
        else
        {
            std::cout << "2\n";
        }
    }
    return 0;
}

이번엔 circleDistance를 double이 아닌 int로 선언해서 틀렸다.

컴파일 에러는 main()함수에서 }를 빼먹어서 난 오류이다.

확실히 c++이 빠르고 메모리도 덜먹는다. Python이랑 C++이랑 똑같은 방법으로 풀었는데 차이가 많이 난다.

'알고리즘 > BOJ' 카테고리의 다른 글

[Python]1194-달이 차오른다, 가자.  (0) 2020.08.10
[Python&C++]7868-해밍 수열  (0) 2020.07.11
[Python&C++]1037-약수  (0) 2020.05.19
[Python&C++]1026-보물  (0) 2020.05.18
[Python&C++]1032-명령 프롬포트  (0) 2020.05.18
[Python&C++]1004-어린 왕자  (0) 2020.05.18
[Python&C++]1003-피보나치 함수  (0) 2020.05.18
[Python&C++]2490-윷놀이  (0) 2020.05.18