728x90
- 문제
https://www.acmicpc.net/problem/1004
1004번: 어린 왕자
입력의 첫 줄에는 테스트 케이스의 개수 T가 주어진다. 그 다음 줄부터 각각의 테스트케이스에 대해 첫째 줄에 출발점 (x1, y1)과 도착점 (x2, y2)이 주어진다. 두 번째 줄에는 행성계의 개수 n이 주
www.acmicpc.net
- 알고리즘 ( 접근 )
오른쪽 점이 출발점, 왼쪽 점이이 도착점이 해보자.
이를 통해 생각해 볼 수 있는 조건이 있다.
i) 출발점과 도착점이 같은 행성 안에 있다면 행성계에 진입/이탈할 이유가 없다.
ii) 출발점과 도착점이 행성에 속해있지 않다면 행성계에 진입/이탈할 이유가 없다.
iii) 둘 중 하나만 행성에 속해있다면 행성계에 진입/이탈해야 한다.
행성계를 반드시 지나는 횟수만 카운트해 출력한다.
- 풀이 - C++ ( C )
더보기
#include <iostream>
using namespace std;
int T, x1, y1, x2, y2, n, cx, cy, r;
int main()
{
cin >> T;
while (T--)
{
cin >> x1 >> y1 >> x2 >> y2; // 출발점(x1, y1)과 도착점(x2,y2)
cin >> n;
int ans = 0;
while (n--)
{
cin >> cx >> cy >> r;
int cnt = 0;
if ((cx- x1) * (cx - x1) + (cy - y1) * (cy - y1) <= r * r)
cnt++;
if ((cx - x2) * (cx - x2) + (cy - y2) * (cy - y2) <= r * r)
cnt++;
if (cnt == 1)ans++;
}
cout << ans << "\n";
}
}
- 풀이 - JAVA
더보기
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T-->0)
{
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
int n = sc.nextInt();
int ans = 0;
while(n-->0)
{
int cx = sc.nextInt();
int cy = sc.nextInt();
int r = sc.nextInt();
int cnt = 0;
int dist1 = (cx - x1) * (cx - x1) + (cy - y1) * (cy - y1);
int dist2 = (cx - x2) * (cx - x2) + (cy - y2) * (cy - y2);
if(dist1 <= r * r) cnt++;
if(dist2 <= r * r) cnt++;
if(cnt == 1) ans++;
}
System.out.println(ans);
}
}
}
- 풀이 - PYTHON
더보기
t = int(input())
for i in range(t):
x1, y1, x2, y2 = list(map(int, input().split()))
n = int(input())
ans = 0
for j in range(n):
cnt = 0
cx, cy, r = list(map(int, input().split()))
dist1 = (cx - x1) ** 2 + (cy - y1) ** 2
dist2 = (cx - x2) ** 2 + (cy - y2) ** 2
if dist1 <= r * r: cnt+=1
if dist2 <= r * r: cnt+=1
if cnt == 1: ans+=1
print(ans)
문제점, 오류, 개선점 댓글 부탁드립니다
728x90
'PS > 백준' 카테고리의 다른 글
[ 백준 ] 7576번 : 토마토 - (c++/c, java/자바, python/파이썬) (0) | 2022.02.14 |
---|---|
[ 백준 ] 1271번 : 엄청난 부자2 - (python/파이썬, java/자바) (0) | 2022.02.02 |
[백준] 1003번 : 피보나치 함수 - (c++, c, python/파이썬, java/자바) (0) | 2022.01.28 |
[ 백준 ] 1002번 : 터렛 - (C++/C , JAVA/자바 , PYTHON/파이썬, Node.js/자바스크립트) (0) | 2022.01.28 |
[백준] 1001번 : A-B - (C++/C, JAVA/자바, PYTHON/파이썬, Node.js/자바스크립트 ) (0) | 2022.01.28 |