236. 유한소수 판별하기

김해준's avatar
Jan 11, 2026
236. 유한소수 판별하기

사용언어 : Java

문제
notion image
풀이
/* a = 7 / b = 20 20 / 2 = 10 ...0 -> O 10 / 2 = 5 ...0 -> O 5 / 2 = 2 ... 1 -> X b % 2 != 0 break; 5 / 5 = 1 ...0 -> O result == 1 || b % 5 != 0 break; if(result == 1)return 1 else return 2 */ class Solution { public int solution(int a, int b) { int gcdA = a / GCD(a,b); int gcdB = b / GCD(a,b); int temp = gcdB; while(true) { if(temp % 2 != 0 || temp == 1) break; temp = temp / 2; } while(true) { if(temp % 5 != 0 || temp == 1) break; temp = temp / 5; } if(temp == 1)return 1; else return 2; } static int GCD(int num1, int num2) { int result = 0; int head; int tail; if (num1 > num2) { head = num1; tail = num2; } else { head = num2; tail = num1; } int temp = tail; while(true) { temp = head % tail; if(temp == 0){ result = tail; break; } head = tail; tail = temp; } return result; } }
notion image
Share article