import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int i = scanner.nextInt(); int j = scanner.nextInt(); int res=1; for (int k = 1; k < Math.min(i, j); k++) { if (i%k==0&&j%k==0) res=Math.max(res,k); } System.out.println(res); } }
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String... as) { Scanner sc = new Scanner(System.in); while (sc.hasNextInt()) System.out.println(gcd(sc.nextInt(), sc.nextInt())); sc.close(); } private static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } }
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y =sc.nextInt();
//简单点,如果求余为0.则为公约数,直到求出最大的公约数。 //从复杂度明显不如辗转相除法。因为一开始并不知道这个算法。 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int s=0; for(int i=1; i <= a&&i <= b; i++) { if(a%i==0 && b%i==0) { s=i; } } System.out.println(s); } }
import java.util.Scanner;
/**
* @author Allen_Hua
* @create_time 创建时间:May 13, 2018 1:45:43 PM 类说明
*/
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
int a = scan.nextInt();
int b = scan.nextInt();
System.out.println(gcd(a, b));
}
}
//辗转相除法求最大公约数 greatest common divisor
private static int gcd(int a, int b) {
// TODO Auto-generated method stub
int max, min, temp;
//将a、b中的较大值给max 较小值给min
if (a > b) {
max = a;
min = b;
} else {
min = a;
max = b;
}
temp = max % min;//大值对小值取模
while (temp != 0) {
max = min;
min = temp;
temp = max % min;
}
return min;
}
}