結果

問題 No.141 魔法少女コバ
ユーザー tenten
提出日時 2020-11-17 20:49:14
言語 Java
(openjdk 23)
結果
AC  
実行時間 136 ms / 5,000 ms
コード長 812 bytes
コンパイル時間 2,083 ms
コンパイル使用メモリ 74,144 KB
実行使用メモリ 55,992 KB
最終ジャッジ日時 2024-07-23 08:27:48
合計ジャッジ時間 16,893 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 93
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        if (n == m) {
            System.out.println(0);
            return;
        }
        int gcd = getGCD(n, m);
        n /= gcd;
        m /= gcd;
        System.out.println(getCount(n, m) - 1);
    }
    
    static int getCount(int x, int y) {
        if (x == 0 || y == 0) {
            return 0;
        }
        if (x > y) {
            return x / y +  getCount(x % y, y);
        } else {
            return 1 + getCount(y, x);
        }
    }
    
    static int getGCD(int x, int y) {
        if (x % y == 0) {
            return y;
        } else {
            return getGCD(y, x % y);
        }
    }
}
0