회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
1
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
왜 틀렸는지 모르겠네요. 도움주세요.
10253번 - 헨리
hjj5612
2년 전
0
입력 데이타가
1
9997 9998
인 경우 무한 루프에 빠지는 것 같은데요. 제가 잘 못 한 건가요?
import java.math.BigInteger; import java.util.ArrayList; import java.util.Scanner; public class Main { private static class Fraction { public BigInteger numerator; public BigInteger denominator; public Fraction(int numerator, int denominator) { this.numerator = new BigInteger(String.valueOf(numerator)); this.denominator = new BigInteger(String.valueOf(denominator)); } public Fraction(BigInteger numerator, BigInteger denominator) { this.numerator = numerator; this.denominator = denominator; } public void subtract(Fraction other) { BigInteger de = denominator.multiply(other.denominator); BigInteger t = numerator.multiply(other.denominator); BigInteger u = other.numerator.multiply(denominator); this.numerator = t.subtract(u); this.denominator = de; } /** * GE : Greater and Equal * @param other * @return * other 보다 크거나 같으면 true, otherwise false */ public boolean isGE(Fraction other) { BigInteger t = numerator.multiply(other.denominator); BigInteger u = other.numerator.multiply(denominator); BigInteger v = t.subtract(u); if(v.signum() == 0 || v.signum() == 1) return true; else return false; } public void reduce() { BigInteger common = gcd(numerator, denominator); numerator = numerator.divide(common); denominator = denominator.divide(common); } } private static BigInteger solve(Fraction fraction) { fraction.reduce(); if(fraction.numerator.equals(BigInteger.ONE)) return fraction.denominator; BigInteger d = BigInteger.TWO; while(true) { Fraction t = new Fraction(BigInteger.ONE, d); if(fraction.isGE(t)) { fraction.subtract(t); fraction.reduce(); if(fraction.numerator.equals(BigInteger.ONE)) return fraction.denominator; } d = d.add(BigInteger.ONE); } } private static BigInteger gcd(BigInteger a, BigInteger b) { if(b.equals(BigInteger.ZERO)) return a; else { BigInteger t = a.mod(b); return gcd(b, t); } } private static ArrayList<Fraction> data; private static boolean input() { boolean state; try(Scanner kb = new Scanner(System.in)) { data = new ArrayList<>(); int n = kb.nextInt(); for(int i = 0; i < n; i++) { int numerator = kb.nextInt(); int denominator = kb.nextInt(); data.add(new Fraction(numerator, denominator)); } state = true; } catch (Exception e) { state = false; } return state; } public static void main(String[] args) { if(input()) { for(Fraction fraction : data) { System.out.println(solve(fraction)); } } } }
댓글을 작성하려면
로그인
해야 합니다.
hjj5612 2년 전
입력 데이타가
1
9997 9998
인 경우 무한 루프에 빠지는 것 같은데요. 제가 잘 못 한 건가요?