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/j/Main11827.java

/*
 * Author: Jeremy Meador
 * Problem: 11827 Maximum GCD
 */


import java.util.Scanner;
import java.util.ArrayList;


public class Main11827
{
  public static int gcd(int a, int b, int max)
  {
    if (a==0||b==0)
      return 0;
    if (a < b)
    {
      int t = a;
      a = b;
      b = t;
    }
    if (b<=max)
      return 0;
    int c = a%b;
    if (c == 0)
    {
      return b;
    }
    return gcd(b,c,max);
  }
  
  public static void main(String[] args)
  {
    Scanner in = new Scanner(System.in);
    int count = in.nextInt();
    in.nextLine();
    while ((count != 0)&&(in.hasNextLine()))
    {
      ArrayList<Integer> nums = new ArrayList<Integer>();
      int max = 0;
      Scanner line = new Scanner(in.nextLine());
      while (line.hasNextInt())
      {
        nums.add(line.nextInt());
      }
      for (int j=0;j<nums.size();j++)
        for (int i=j+1;i<nums.size();i++)
        {
          int g;
          if ((g = gcd(nums.get(j),nums.get(i), max)) != 0)
          {
            max = g;
          }
        }
      System.out.println(max);
      count--;
    }
  }
}






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