计算gcd

package hello;//JAVA程序在SRC里无需此句


import java.util.Scanner;
public class Hello {
	public static void main(String[]  args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		int number;
		number = input.nextInt();
		System.out.println(gcd(new int[number]));
}
	public static int gcd(int... numbers) {
		Scanner input = new Scanner(System.in);
		int max;
		for(int i = 0; i < numbers.length; ++i) {
			numbers[i] = input.nextInt();
		}
		max = gtDivisor(numbers[0],numbers[1]);
		for(int i = 0; i < numbers.length - 1; i++) {
			max = gtDivisor(numbers[i],numbers[i + 1]);
	}
		return max;
	}
	public static int gtDivisor(int a, int b) {
		int temp = 1;
		if(a < b) {
			temp = a;
			a = b;
			b = temp;
		}
		if(a % b == 0)
			return b;
		while(temp != 0) {
			temp = a % b;
			a = b;
			b = temp;
		}
		int max;
		max = a;
		return max;
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_39596963/article/details/79319284
gcd
今日推荐