자바로 백준 1358 풀기

hong030·2023년 4월 14일
0
  • 실버 4단계 문제

풀이)

사람의 좌표값과 하키장의 좌표가 주어질 때, 몇 명의 사람이 하키장에 있는지 수를 구하여라.

사람이 하키장에 있는 경우는 3가지이다.
1. (x,y)~(x+width, y+height) 직사각형 면적 내에 사람의 좌표가 있을 경우.
2. (x,y+rad)~(사람의 좌표) 길이가 rad보다 작거나 같을 경우
3. (x+width, y+rad)~(사람의 좌표) 길이가 rad보다 작거나 같을 경우.

내 코드)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Backjoon1358 {
	static double getDist(int x1, int y1, int x2, int y2) {
		return Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
	}
	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(bf.readLine());
		int width = Integer.parseInt(st.nextToken());
		int height = Integer.parseInt(st.nextToken());
		int rad = height/2;
		int x = Integer.parseInt(st.nextToken());
		int y = Integer.parseInt(st.nextToken());
		int player = Integer.parseInt(st.nextToken());
		
		int loc[][] = new int[player][2];		
		for(int i=0;i<player;i++) {
			st = new StringTokenizer(bf.readLine());
			loc[i][0] = Integer.parseInt(st.nextToken());
			loc[i][1] = Integer.parseInt(st.nextToken());
		}
		int count = 0;
		
		//double dist1 = ;
		//double dist2 = ;
		for(int i=0;i<player;i++) {
			if ((loc[i][0]>=x && loc[i][0]<=(x+width)) && (loc[i][1]>=y && loc[i][1]<=(y+height))) {
				count++;
			}else if (getDist(x, y+rad, loc[i][0], loc[i][1])<=rad) {
				count++;
			}else if (getDist(x+width, y+rad, loc[i][0], loc[i][1])<=rad) {
				count++;
			}else {
				continue;
			}
		}
		System.out.println(count);
	}
}

profile
자바 주력, 프론트 공부 중인 초보 개발자. / https://github.com/hongjaewonP

0개의 댓글