java实现最大公约数

package shi_yan;

import java.util.Scanner;

public class gongbei_gongyue {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		int a = scanner.nextInt();
		int b = scanner.nextInt();
		System.out.println(gcd1(a,b));
		System.out.println(gcd2(a,b));
}
	//(1)循环迭代
			public static int gcd1(int a, int b) 
			{ 
			    while (b!=0) 
			    { 
			        int temp=b; 
			        b = a%b; 
			        a = temp; 
			    } 
			    return a; 
			}
			
			//(2)递归实现

		public static	int gcd2(int a, int b) 
			{ 
			    if (b==0) 
			       return a; 
			    else 
			       return gcd2(b,a%b); 
			}

		}



你可能感兴趣的:(java)