Coding Test/백준
[백준 1008번 - java] A/B
olli2
2021. 11. 2. 16:22
내 답안 (오답)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int A = s.nextInt();
int B = s.nextInt();
System.out.println(A/B);
}
}
<제약조건이자 단서>
"상대오차가 10⁻⁹ 이내이면 정답이다."
<해결 방법>
변수를 소수점 아래 9자리 이상 표현할 수 있는 실수형(double)으로 입력받도록 한다.
int형으로 입력받아 계산할 경우, 소수점 아래 단위를 버리고 계산하기 때문에 double형(실수형)으로 입력받아 계산해야 한다.
정답
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
double A = s.nextInt();
double B = s.nextInt();
System.out.println(A/B);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
double A = s.nextDouble();
double B = s.nextDouble();
System.out.println(A/B);
}
}
import java.util.*;
import java.io.*;
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);
}
}