結果

問題 No.141 魔法少女コバ
ユーザー jp_ste
提出日時 2015-03-22 13:34:33
言語 Java
(openjdk 23)
結果
AC  
実行時間 125 ms / 5,000 ms
コード長 823 bytes
コンパイル時間 1,843 ms
コンパイル使用メモリ 73,992 KB
実行使用メモリ 41,868 KB
最終ジャッジ日時 2024-06-29 00:01:37
合計ジャッジ時間 15,093 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 93
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int M = sc.nextInt();
		int N = sc.nextInt();
		
		if(M == N)  { 
			System.out.println(0);
			return;
		}
		
		int div = getCommonDivisor(M, N);
		int m = M/=div;
		int n = N/=div;
		int ans = 0;
	
		while(true) {			
			if(m==1 || n==1) break;
			
			if(m>n) {
				m-=n; 
				ans++;
			} else {
				int tmp=m;
				m=n;
				n=tmp;
				ans++;
			}
		}
		
		if(m==1) {
			ans++; 
			ans += (n-1);
			
		} else if(n==1) {
			ans += (m-1);
		}
		System.out.println(ans);
	}
	
	static int getCommonDivisor(final int a, final int b) {
		
		int nowA = a;
		int nowB = b;
		
		while(true) {
			int tmp = nowB % nowA;
			if(tmp == 0) break;
			
			nowB = nowA;
			nowA = tmp;
		}
		return nowA;
	}
}
0