結果

問題 No.141 魔法少女コバ
ユーザー kenkoooo
提出日時 2015-03-14 03:16:20
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,181 ms / 5,000 ms
コード長 1,002 bytes
コンパイル時間 1,922 ms
コンパイル使用メモリ 77,216 KB
実行使用メモリ 37,352 KB
最終ジャッジ日時 2024-06-28 22:52:26
合計ジャッジ時間 14,756 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 93
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;

public class Main {

	public static void main(String[] args) {
		int M = nextInt();
		int N = nextInt();
		int gcd = gcd(M, N);
		M /= gcd;
		N /= gcd;

		int magic = 0;
		while (N != M || N != 1) {
			if (M < N) {
				int tmp = M;
				M = N;
				N = tmp;
			} else {
				M -= N;
			}
			magic++;
		}
		System.out.println(magic);

	}

	static int gcd(int a, int b) {
		// b=0ならaを最大公約数として終了
		if (b == 0) {
			return a;
		} else {
			// a÷bのあまりをb、元のbをaとして2.にもどる
			return gcd(b, a % b);
		}
	}

	static int nextInt() {
		int c;
		try {
			c = System.in.read();
			while (c != '-' && (c < '0' || c > '9'))
				c = System.in.read();
			if (c == '-')
				return -nextInt();
			int res = 0;
			while (c >= '0' && c <= '9') {
				res = res * 10 + c - '0';
				c = System.in.read();
			}
			return res;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}

}
0