(UVA) Tour de France - Solution

Brute Force was used to solve this problem.


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

class Main {
    public static int reader(BufferedReader br) throws NumberFormatException, IOException {    
        int n;
        int resp = 0;    
     
        while (true) {        
            n = br.read();        
            if (n >= '0' && n <= '9') {
                break;
            }
        }
          
        while (true) {        
            resp = resp*10 + n-'0';        
            n = br.read();        
            if (n < '0' || n > '9') {
                break;    
            }
        }
     
        return resp;    
    }
  
    public static void process() throws NumberFormatException, IOException { 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      
        String line = br.readLine();
        String[] s = line.split("\\s");
        while (s.length > 1) {
            int numSprocketsFront = Integer.parseInt(s[0]);
            int numSprocketsRear = Integer.parseInt(s[1]);
          
            int[] frontSprockets = new int[numSprocketsFront];
            for (int i = 0; i < numSprocketsFront; i++) {
                frontSprockets[i] = reader(br);
            }
          
            int[] rearSprockets = new int[numSprocketsRear];
            for (int i = 0; i < numSprocketsRear; i++) {
                rearSprockets[i] = reader(br);
            }
          
            ArrayList<Double> ratios = new ArrayList<Double>();
            for (int i = 0; i < numSprocketsFront; i++) {
                for (int j = 0; j < numSprocketsRear; j++) {
                    ratios.add(((double)rearSprockets[j])/frontSprockets[i]);
                }
            }
          
            Collections.sort(ratios);
            double max = -1.0;
            for (int i = 1; i < ratios.size(); i++) {
                double d = ratios.get(i)/ratios.get(i-1);
                if (d > max) {
                    max = d;
                }
            }
          
            System.out.printf("%.2f\n", max);
          
            line = br.readLine();
            s = line.split("\\s");
        }
                  
        return;
    }
 
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();

        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Powers of Two - Solução

(Coderbyte) Dash Insert II - Solução

(CoderByte) Number Search - Solução