Competitive/Collaborative Programming Class

ICPC Computer Programming Contest Prep

Problem Solving in Computer Science

Fall 2011 -- CSC 2700 Section 02

[Isaac's Home Page ]  [Mailing List ]  [Class Page ]  [Normal ]  

4/b/Main.java

/*
 * Author: Jonathan Broome
 * Problem: UVA 11827
 */

import java.util.*;

public class Main {
	
	static int max;
	
	public static int gcd(int a, int b) {
		if(b == 0)
			return a;
		if(a == 0)
			return b;
		else
			return gcd(b, a%b);
			
	}
	
	public static void main(String[] args) {
     	Scanner in = new Scanner(System.in);
		
		int n = in.nextInt();
		int[] ans = new int[n];
		in.nextLine();
		
		for(int i=0; i<n; i++) {
         	max = 1;
			String str = in.nextLine();
            String[] strs = str.split("\\s");
			int[] nums = new int[strs.length];
			for(int j=0;j<nums.length;j++) {
             	nums[j] = Integer.parseInt(strs[j]);
			}
            int x = 1;
			for(int j=0;j<nums.length;j++) {
             	for(int k=j+1; k<nums.length;k++) {
                 	x = gcd(nums[j], nums[k]);
				}
				if(x > max)
					max = x;
			}
			ans[i] = max;
		}
		
		for(int i=0;i<ans.length;i++) {
         	System.out.println(ans[i]);
		}
	}
}

The statements and opinions included in these pages are those of only. Any statements and opinions included in these pages are not those of Louisiana State University or the LSU Board of Supervisors.
© 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Isaac Traxler