下面是使用递归实现gcd(最大公约数)
package com.study.text;
import java.util.Scanner;
public class Text19 {
public static int gcd(int x,int y)
{
int temp;
if(x>y)
{
temp=x;
x=y;
y=temp;
}
if(y%x==0)
return x;
else
{
int flag=x;
x=y%x;
y=flag;
return gcd(x,y);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner rd=new Scanner(System.in);
int a,b;
a=rd.nextInt();
b=rd.nextInt();
System.out.println(gcd(a,b));
}
}