자바/자바의정석-연습문제

자바의정석 연습문제 풀이 6장 6-6

김발자~ 2022. 9. 22. 21:06
반응형

자바의 정석 3판 연습문제 풀이

 

연습문제 파일은 아래 링크에서 다운받을 수 있다

https://github.com/castello/javajungsuk3/tree/master/%EC%97%B0%EC%8A%B5%EB%AC%B8%EC%A0%9C%ED%92%80%EC%9D%B4

 

GitHub - castello/javajungsuk3: soure codes and ppt files of javajungsuk 3rd edition

soure codes and ppt files of javajungsuk 3rd edition - GitHub - castello/javajungsuk3: soure codes and ppt files of javajungsuk 3rd edition

github.com

 

 

http://www.yes24.com/Product/Goods/24259565

 

Java의 정석 - YES24

최근 7년동안 자바 분야의 베스트 셀러 1위를 지켜온 `자바의 정석`의 최신판. 저자가 카페에서 12년간 직접 독자들에게 답변을 해오면서 초보자가 어려워하는 부분을 잘 파악하고 쓴 책. 뿐만 아

www.yes24.com

자바의 정석 저자 남궁성 님이 직접 올려주신 문제다

 

 


챕터6은 총 24문제로 구성되어 있다


6-6


 

1
2
3
4
5
6
7
8
9
10
11
12
public class Practice_6_6_7 {
 
    static double getDistance(int x, int y, int x1, int y1) {
        //두 점 사이의 거리 d^2 = (x1 - x)^2 + (y1 - y)^2
        // d = 루트 ((x1 - x)^2 + (y1 - y)^2)
        return Math.sqrt(Math.pow(x1 - x, 2+ Math.pow(y1 - y, 2));
    }
    
    public static void main(String[] args) {
        System.out.println(getDistance(1122));
    }
}
cs

 

주석으로 달아놓긴 했지만

두 점 사이의 거리 d = 루트((x1 - x2)^2 + (y1-y2)^2)

 

단순히 2제곱이라 Math.pow(a, n): a의 n제곱 메서드 사용 없이

(x1 - x)*(x1 - x)으로 표현했어도 괜찮다(메서드를 덜 사용하는 것이 적은 비용을 들여 만들 수 있다)

 

Math.sqrt 는 제곱근(루트)

Math.pow 는 제곱

 

 


 

 
반응형