Java/Java 알고리즘

백준 1312번 소수점 아래 N번째 수 JAVA 구현해보기

kimc 2022. 7. 14. 20:55

```

백준 1312번 소수점 아래 N번째 수 JAVA 구현해보기

```

이번 글을 통해 배워갈 내용

  1. 백준 1312번 풀이

https://www.acmicpc.net/problem/1312

 

1312번: 소수

피제수(분자) A와 제수(분모) B가 있다. 두 수를 나누었을 때, 소숫점 아래 N번째 자리수를 구하려고 한다. 예를 들어, A=3, B=4, N=1이라면, A÷B=0.75 이므로 출력 값은 7이 된다.

www.acmicpc.net

 

백준 1312번은

난이도 브론즈 등급의 문제로서

 

분자 A, 분모 B, 자릿수 N이 주어지면

A / B 했을 때, 소수점 아래 N번째 수를 출력하면 되는 문제입니다.

 


30분 정도 위에 링크를 방문하셔서 풀어보시고

안 풀리시는 경우에만 아래 해답을 봐주시면 감사하겠습니다.

손으로 나눗셈을 할 때

분모를 분자로 나누고

나눈 값에 10을 곱해서 또 나누고 해서

N번째 자릿수의 숫자를 구했습니다.

옛날 초등학교 시절의 추억이 생각나는 문제였습니다. 

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        final String[] input = br.readLine().split(" ");
        final int numerator = Integer.parseInt(input[0]);
        final int denominator = Integer.parseInt(input[1]);
        final int n = Integer.parseInt(input[2]);
        System.out.print(solution(numerator, denominator, n));
    }

    private static int solution(int numerator, int denominator, int n) {
        int nthDigit = 0;

        if (numerator % denominator != 0) {
            if (numerator > denominator) {
                numerator = numerator % denominator;
            }

            for (int i = 0; i < (n - 1); i++) {
                numerator *= 10;
                numerator = numerator % denominator;
            }

            numerator *= 10;
            nthDigit = (numerator / denominator);
        }
        return nthDigit;
    }

}

// https://codemasterkimc.tistory.com

 

 

읽어주셔서 감사합니다

 

무엇인가 얻어가셨기를 바라며

 

오늘도 즐거운 코딩 하시길 바랍니다 ~ :)

 


 

728x90