[문제]
직각삼각형의 한 변의 길이를 나타내는 정수 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
'Coding Practice > 프로그래머스 Lv.0' 카테고리의 다른 글
특수문자 출력하기 (0) | 2024.01.18 |
---|---|
대소문자 바꿔서 출력 (0) | 2024.01.18 |
창고정리 (0) | 2023.12.21 |
가습기 (0) | 2023.12.20 |
가채점 (1) | 2023.12.20 |