Last active
June 23, 2017 08:08
-
-
Save sekky0905/8642d420caa3a251272478534c52b5c7 to your computer and use it in GitHub Desktop.
Javaでユーグリッドの互除法を実装してみた ref: http://qiita.com/Sekky0905/items/d7f68b3d37bd7cdab54f
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a % b = r | |
b % r = s | |
r % s = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.io.*; | |
class Main { | |
public static void main(String[] args) { | |
try { | |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); | |
String[] str = bufferedReader.readLine().split(" "); | |
int x = Integer.parseInt(str[0]); | |
int y = Integer.parseInt(str[1]); | |
System.out.println(getCommonDivisor(x, y)); | |
} catch (Exception e) { | |
System.out.println(e); | |
} | |
} | |
private static int getCommonDivisor(int x, int y) { | |
int biggerNum = Math.max(x, y); | |
int smallerNum = Math.min(x, y); | |
// 大きい方から小さい方を割った余を求める | |
int surplus = biggerNum % smallerNum; | |
// 割り切れていれば、それを返す | |
if (surplus == 0) { | |
return smallerNum; | |
} | |
// 割り切れなければ再帰的に自信を呼び出す | |
surplus = getCommonDivisor(smallerNum, surplus); | |
return surplus; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 入力 | |
390 273 | |
// 出力 | |
39 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.io.*; | |
class Main { | |
private static int x = -1; | |
private static int y = -1; | |
private static final String caution = "自然数を半角空白区切りで2つ入力してください(ただし、本プログラムでは自然に0は含めないものとする)"; | |
public static void main(String[] args) { | |
System.out.println(caution); | |
readInput(); | |
System.out.println(doEuclideanAlgorithm(x, y)); | |
} | |
private static void readInput() { | |
try { | |
while (x <= 0 || y <= 0) { | |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); | |
String[] str = bufferedReader.readLine().split(" "); | |
x = Integer.parseInt(str[0]); | |
y = Integer.parseInt(str[1]); | |
if (x <= 0 || y <= 0) { | |
System.out.println("入力が不適切です。" + caution); | |
} | |
} | |
} catch (Exception e) { | |
System.out.println("入力が不適切です。" + caution); | |
readInput(); | |
} | |
} | |
private static int doEuclideanAlgorithm(int x, int y) { | |
int biggerNum = Math.max(x, y); | |
int smallerNum = Math.min(x, y); | |
// 大きい方から小さい方を割った余を求める | |
int surplus = biggerNum % smallerNum; | |
// 割り切れていれば、それを返す | |
if (surplus == 0) { | |
return smallerNum; | |
} | |
// 割り切れなければ再帰的に自信を呼び出す | |
surplus = doEuclideanAlgorithm(smallerNum, surplus); | |
return surplus; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
自然数を半角空白区切りで2つ入力してください(ただし、本プログラムでは自然に0は含めないものとする) | |
a a | |
入力が不適切です。自然数を半角空白区切りで2つ入力してください(ただし、本プログラムでは自然に0は含めないものとする) | |
390 0 | |
入力が不適切です。自然数を半角空白区切りで2つ入力してください(ただし、本プログラムでは自然に0は含めないものとする) | |
0 273 | |
入力が不適切です。自然数を半角空白区切りで2つ入力してください(ただし、本プログラムでは自然に0は含めないものとする) | |
-390 273 | |
入力が不適切です。自然数を半角空白区切りで2つ入力してください(ただし、本プログラムでは自然に0は含めないものとする) | |
390 -273 | |
入力が不適切です。自然数を半角空白区切りで2つ入力してください(ただし、本プログラムでは自然に0は含めないものとする) | |
390 273 | |
39 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.io.*; | |
class Main { | |
private static final String caution = "自然数を半角空白区切りで2つ入力してください(ただし、本プログラムでは自然に0は含めないものとする)"; | |
public static void main(String[] args) { | |
System.out.println(caution); | |
int[] inputs = readInput(); | |
System.out.println(doEuclideanAlgorithm(inputs[0], inputs[1])); | |
} | |
private static int[] readInput() { | |
try { | |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); | |
String[] str = bufferedReader.readLine().split(" "); | |
int x = Integer.parseInt(str[0]); | |
int y = Integer.parseInt(str[1]); | |
if (x <= 0 || y <= 0) { | |
throw new Exception(""); | |
} | |
return new int[]{x, y}; | |
} catch (Exception e) { | |
System.out.println("入力が不適切です。" + caution); | |
return readInput(); | |
} | |
} | |
private static int doEuclideanAlgorithm(int x, int y) { | |
int biggerNum = Math.max(x, y); | |
int smallerNum = Math.min(x, y); | |
// 大きい方から小さい方を割った余を求める | |
int surplus = biggerNum % smallerNum; | |
// 割り切れていれば、それを返す | |
if (surplus == 0) { | |
return smallerNum; | |
} | |
// 割り切れなければ再帰的に自信を呼び出す | |
surplus = doEuclideanAlgorithm(smallerNum, surplus); | |
return surplus; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
自然数を半角空白区切りで2つ入力してください(ただし、本プログラムでは自然に0は含めないものとする) | |
a a | |
入力が不適切です。自然数を半角空白区切りで2つ入力してください(ただし、本プログラムでは自然に0は含めないものとする) | |
390 0 | |
入力が不適切です。自然数を半角空白区切りで2つ入力してください(ただし、本プログラムでは自然に0は含めないものとする) | |
0 273 | |
入力が不適切です。自然数を半角空白区切りで2つ入力してください(ただし、本プログラムでは自然に0は含めないものとする) | |
-390 273 | |
入力が不適切です。自然数を半角空白区切りで2つ入力してください(ただし、本プログラムでは自然に0は含めないものとする) | |
390 -273 | |
入力が不適切です。自然数を半角空白区切りで2つ入力してください(ただし、本プログラムでは自然に0は含めないものとする) | |
390 273 | |
39 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment