Coding Practice/프로그래머스 Lv.0

피타고라스의 정리

ailen22 2023. 12. 11. 20:10

[문제]

직각삼각형의 한 변의 길이를 나타내는 정수 a와 빗변의 길이를 나타내는 정수 c가 주어질 때, 다른 한 변의 길이의 제곱, b_square 을 출력하도록 한 줄을 수정해 코드를 완성해 주세요.

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int c = sc.nextInt();

        int b_square = c - a;

        System.out.println(b_square);
    }
}

 

 

 

[답] 

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int c = sc.nextInt();

        int b_square = (int) Math.pow(c, 2) - (int) Math.pow(a, 2);

        System.out.println(b_square);
    }
}

 

Java에서 제곱근을 나타내는 방법인 Math.pow()함수를 이용하여 답을 작성

 

Math.pow()함수 앞에 int함수를 안 쓸 경우 에러 발생

error: possible lossy conversion from double to int