UVa - 11827 Maximum GCD

//java solution

import java.io.*;
import java.util.*;

class Main
{


    public static void main (String args[])  // entry point from OS
    {
        Main myWork = new Main();  // create a dinamic instance
        myWork.Begin();            // the true entry point
    }
int gcd(int a,int b) {
while (b > 0) {
a = a % b;
b=a-b+(a=b); }
return a;
}
    void Begin()
    {
 try
 {
 
Scanner sc=new Scanner(System.in);
int n;
ArrayList dts,a;
dts=new ArrayList(150);
a=new ArrayList(150);
n=Integer.parseInt(sc.nextLine());
for(int i=0;i<n;i++)
{
dts.add(sc.nextLine());
}
for(int k=0;k<n;k++)
{
Scanner scc=new Scanner(dts.get(k).toString());
while(scc.hasNext()) a.add(scc.next());
int max=1,t,t1,t2;
for(int i=0;i<a.size();i++)
for(int j=i+1;j<a.size();j++)
{
t1=Integer.parseInt(a.get(i).toString());
t2=Integer.parseInt(a.get(j).toString());
t=gcd(t1,t2);
if(max<t) max=t;
}
System.out.println(max);
a.clear();
}
sc.close();
}
 catch(Exception e)
 {
  System.exit(0);
 }
    }


}

No comments:

Post a Comment