http://ac.jobdu.com/problem.php?pid=1056
-
题目描述:
-
输入两个正整数,求其最大公约数。
-
输入:
-
测试数据有多组,每组输入两个正整数。
-
输出:
-
对于每组输入,请输出其最大公约数。
-
样例输入:
-
49 14
-
样例输出:
-
7
import
java.util.Scanner;
public
class
Main {
public
static
void
main(String[] args) {
Scanner sc=
new
Scanner(System.in);
while
(sc.hasNext()){
int
m=sc.nextInt();
int
n=sc.nextInt();
System.out.println(GCD(m,n));
}
}
public
static
int
GCD(
int
m,
int
n){
while
(m!=
0
&&n!=
0
){
if
(m>n)
m=m%n;
else
n=n%m;
}
if
(m==
0
)
return
n;
else
return
m;
}
}