문제 링크: https://www.acmicpc.net/problem/1004
백준 기하 1 7단계 - 1004번 어린 왕자를 풀어보았다.
풀이: 행성계를 몇 번 통과하는지 계산하는 문제이다.
C++
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int cnt=0;
int x1,y1,x2,y2,n;
cin>>x1>>y1>>x2>>y2;
cin>>n;
vector<int> cx(n);
vector<int> cy(n);
vector<int> cr(n);
for(int j=0;j<n;j++) cin>>cx[j]>>cy[j]>>cr[j];
for(int j=0;j<n;j++)
{
double sdis=pow(pow(x1-cx[j],2)+pow(y1-cy[j],2),0.5);
double edis=pow(pow(x2-cx[j],2)+pow(y2-cy[j],2),0.5);
double r=cr[j];
if(sdis<r)
{
if(r<edis) cnt++;
}
else if(r>edis) cnt++;
}
cout<<cnt<<"\n";
}
}
Python
t=int(input())
for i in range(t):
x1,y1,x2,y2=map(int,input().split())
n=int(input())
x=[]
y=[]
r=[]
cnt=0
for j in range(n):
cx,cy,cr=map(int,input().split()) # 행성계
x.append(cx)
y.append(cy)
r.append(cr)
for j in range(n):
stadis=((x1-x[j])**2+(y1-y[j])**2)**0.5 # 시작점부터 행성계 중심까지 거리
enddis=((x2-x[j])**2+(y2-y[j])**2)**0.5 # 도착점부터 행성계 중심까지 거리
rad=r[j] # 행성계 반지름
if stadis<rad:
if rad<enddis: cnt+=1
elif rad>enddis: cnt+=1
print(cnt)
Java
'코테용 문제풀이 > 백준' 카테고리의 다른 글
약수 풀이 (0) | 2023.01.09 |
---|---|
배수와 약수 풀이 (0) | 2023.01.09 |
터렛 풀이 (0) | 2023.01.09 |
택시 기하학 풀이 (0) | 2023.01.09 |
참외밭 풀이 (0) | 2023.01.09 |