알고리즘 (with JAVA)/코딩 테스트 기출 문제
순환 소수 찾기
백_곰
2022. 8. 28. 17:38
1. 문제 설명
(1) 분수 a/b가 주어질 때 실수 연산을 쓰지 않고 이 분수를 소수 형태로 출력한다.
(2) 예를 들면, 3/8은 0인데 0.375를 출력하고 4712/400은 11인데 11.78을 출력하는 것이다.
2. 입출력 조건 및 예제
입력 조건
(1) a>=0, b>0으로 가정한다.
출력 조건
X
입력 예제
2
3 8
4712 400
출력 예제
0.375
11.78
3. 제약 조건
X
4. 가정법
X
5. 기저 사례
X
6. 코드
public class Main {
private static BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
private static StringTokenizer st;
private static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws Exception {
int c = Integer.parseInt(sc.readLine());
for(int i=0; i<c; i++){
st = new StringTokenizer(sc.readLine());
int N = st.countTokens();
int[] A = new int[N];
for(int j=0; j<N; j++) {
A[j] = Integer.parseInt(st.nextToken());
}
calDiv(A[0],A[1]);
}
System.out.println("\n"+sb);
}
private static void calDiv(int A, int B) {
int index=0;
while(A>0) {
if(index++ == 1) sb.append(".");
sb.append(A/B);
A = (A%B)*10;
}
sb.append("\n");
}
}