10814 - Simplifying Fractions

All about problems in Volume 108. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

AmitMist
New poster
Posts: 6
Joined: Wed Feb 18, 2009 10:20 pm

Re: 10814 - Simplifying Fractions

Post by AmitMist » Tue Aug 03, 2010 10:29 pm

hi, I am trying to use java for BigInteger class. But getting endless WA . I have already tried the given sample inputs. any kind of suggestion would be appreciable :oops: :roll: :o :-?

Code: Select all

/v108-htm01/10814.html

import java.io.*;
import java.util.*;
import java.math.BigInteger;

class Main
{
    static String ReadLn (int maxLg)  // utility function to read from stdin
    {
        byte lin[] = new byte [maxLg];
        int lg = 0, car = -1;
        String line = "";

        try
        {
            while (lg < maxLg)
            {
                car = System.in.read();
                if ((car < 0) || (car == '\n')) break;
                lin [lg++] += car;
            }
        }
        catch (IOException e)
        {
            return (null);
        }

        if ((car < 0) && (lg == 0)) return (null);  // eof
        return (new String (lin, 0, lg));
    }

 public static void main (String args[])  // entry point from OS
    {
        try
        	{
        		Main myWork = new Main();  // create a dinamic instance
        		myWork.Begin();            // the true entry point
        		System.exit(0);
        		
        	}
        	catch(Exception ex)
        	{
        		System.exit(0);
        	}
    }

    void Begin()
    {
        String input;
        StringTokenizer idata;
        int a, b, min, max, num, n, cycle, cyclemax;
        BigInteger z= new BigInteger("0");
   
        input= Main.ReadLn(255);
            idata = new StringTokenizer (input);
          a = Integer.parseInt (idata.nextToken());
            input= Main.ReadLn(255);


        while (a!=0)
        {
            input = Main.ReadLn (255);
         
            idata = new StringTokenizer (input);
           BigInteger aa = new BigInteger ( idata.nextToken(" /") );
           BigInteger bb = new BigInteger ( idata.nextToken(" /") );
          
           BigInteger aaa= new BigInteger(aa.toString());
           BigInteger bbb= new BigInteger(bb.toString());
          
          while(bb.compareTo(z)==1)
          {
          	aa = aa.mod(bb);
          	aa=aa.xor(bb);
          	bb= bb.xor(aa);
          	aa=aa.xor(bb);
          }
          System.out.print(aaa.divide(aa)+" / "+bbb.divide(aa)+"\n");
          a--;

        }
    }
}



Post Reply

Return to “Volume 108 (10800-10899)”