코딩테스트/백준(JAVA)

백준 코딩테스트 1-5-1008 A/B 코딩 문제 java로 푸는 방법

쏠솔랄라 2023. 3. 5. 11:34

 

 

백준 코딩테스트 단계별로 풀어보기

1단계 입출력과 사칙연산의 5단계 1008번 문제입니다

 

 

https://www.acmicpc.net/step

 

단계별로 풀어보기

단계별은 @jh05013님이 관리하고 계십니다. 단계제목설명정보총 문제내가 맞은 문제1입출력과 사칙연산입력, 출력과 사칙연산을 연습해 봅시다. Hello World!132조건문if 등의 조건문을 사용해 봅시다

www.acmicpc.net

 

 


 

 

 

 

문제
두 정수 A와 B를 입력받은 다음, A/B를 출력하는 프로그램을 작성하시오.

입력
첫째 줄에 A와 B가 주어진다. (0 < A, B < 10)

출력
첫째 줄에 A/B를 출력한다. 실제 정답과 출력값의 절대오차 또는 상대오차가 10-9 이하이면 정답이다.

 

 

정답

import java.util.Scanner;

public class Main {

 

public static void main (String[] args) {

 

Scanner sc = new Scanner(System.in);

 

int A = sc.nextInt();

int B = sc.nextInt();

 

System.out.println((double)A/B);

}

}

 

=> 갑자기 등장한 절대오차, 상대오차

double은 실수형 상수로 유효자리수로 오차를 표현하지 않는 자리수이기 때문에

값을 double로 지정하면 해결

 

↓↓↓ 관련포스팅 ↓↓↓

https://developernew.tistory.com/9

 

JAVA 정수형 상수 VS 실수형 상수

정수형 상수 : 정수형 상수는 기본적으로 int 형으로 인식 System.out.println(10); System.out.println(123); 정수 자료형 ; int byte : 1byte -128 ~ 127 -2^7 ~ 2^7 - 1 short : 2byte -32768 ~ 32767 -2^15 ~ 2^15 - 1 int : 4byte -2147483648

developernew.tistory.com

 

 


 

 

1-1-2557 https://developernew.tistory.com/41 <- 백준 답지 제출 방법
1-2-1000 https://developernew.tistory.com/42
1-3-1001 https://developernew.tistory.com/43
1-4-10998 https://developernew.tistory.com/44