标签:
1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 System.out.print("Enter 5 numbers: "); 9 int[] number = new int[5]; 10 11 for(int i = 0; i < 5; i++) 12 number[i] = input.nextInt(); 13 14 input.close(); 15 16 System.out.println("The gcd of this 5 numbers is " + gcd(number)); 17 } 18 19 public static int gcd(int[] array) 20 { 21 int returnValue = 1; 22 int minValue = array[0]; 23 for(int i = 0; i < array.length; i++) 24 if(minValue > array[i]) 25 minValue = array[i]; 26 for(int i = 2; i <= minValue; i++) 27 if(canDivide(array, i)) 28 returnValue = i; 29 return returnValue; 30 } 31 32 public static boolean canDivide(int[] array, int number) 33 { 34 boolean flag = true; 35 for(int i: array) 36 if(i % number != 0) 37 flag = false; 38 return flag; 39 } 40 }
标签:
原文地址:http://www.cnblogs.com/wood-python/p/5808673.html