Created
August 20, 2014 19:56
-
-
Save AIRAT1/bb8082c4480ab52cbae1 to your computer and use it in GitHub Desktop.
package com.javarush.test.level14.lesson08.bonus02;
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.javarush.test.level14.lesson08.bonus02; | |
/* НОД | |
Наибольший общий делитель (НОД). | |
Ввести с клавиатуры 2 целых положительных числа. | |
Вывести в консоль наибольший общий делитель. | |
*/ | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.util.ArrayList; | |
public class Solution | |
{ | |
public static void main(String[] args) throws Exception | |
{ | |
ArrayList<Integer> list = new ArrayList<Integer>(); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | |
int zahl1 = Integer.parseInt(reader.readLine()); | |
int zahl2 = Integer.parseInt(reader.readLine()); | |
int mod = 1; | |
int min = 0; | |
int max = 2147483647; | |
boolean b = true; | |
while (true) { | |
if (zahl1 < 0 || zahl2 < 0) { | |
System.out.println("Zahle muesens groeser als null sein"); | |
break; | |
}else if (zahl1 < zahl2) { | |
min = zahl1; | |
max = zahl2; | |
}else if (zahl2 < zahl1) { | |
min = zahl2; | |
max = zahl1; | |
}else { | |
mod = zahl1; | |
} | |
// System.out.println("min is " + min); | |
// System.out.println("max is " + max); | |
//break; | |
for (int i = min; i > 0; i--) { | |
if (min % i == 0) { | |
list.add(i); | |
} | |
} | |
for (int i : list) { | |
if (max % i == 0) { | |
mod = i; | |
break; | |
} | |
//System.out.println(i); | |
} | |
break; | |
} | |
System.out.println(mod); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Die Aufgabe war gar nicht schwer.