Problem Links:
UVa11242Problem:
Problem D: Tour de France
A racing bicycle is driven by a chain connecting two sprockets. Sprockets are grouped into two clusters: the front cluster (typically consisting of 2 or 3 sprockets) and the rear cluster (typically consisting of between 5 and 10 sprockets). At any time the chain connects one of the front sprockets to one of the rear sprockets. The drive ratio -- the ratio of the angular velocity of the pedals to that of the wheels -- is n:m where n is the number of teeth on the rear sprocket and m is the number of teeth on the front sprocket. Two drive ratios d1<d2 are adjacent if there is no other drive ratio d1<d3<d2. The spread between a pair of drive ratios d1<d2 is their quotient: d2/d1. You are to compute the maximum spread between two adjacent drive ratios achieved by a particular pair of front and rear clusters.Input consists of several test cases, followed by a line containing 0. Each test case is specified by the following input:- f: the number of sprockets in the front cluster;
- r: the number of sprockets in the rear cluster;
- f integers, each giving the number of teeth on one of the gears in the front cluster;
- r integers, each giving the number of teeth on one of the gears in the rear cluster.
Sample Input
2 4 40 50 12 14 16 19 0
Output for Sample Input
1.19
Solution:
Ad-hoc.Generate all of the possible ratios, then calculated the maximum.
Source Code:
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Collections; import java.util.LinkedList; /** * ad-hoc. */ /** * @author antonio081014 * @time Jan 31, 2013, 10:35:28 PM */ public class Main { public static void main(String[] args) throws Exception { Main main = new Main(); main.run(); System.exit(0); } public void run() throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = null; while ((line = br.readLine()) != null && line.compareTo("0") != 0) { String[] strs = line.split("\\s"); int[] front = new int[Integer.parseInt(strs[0])]; int[] rear = new int[Integer.parseInt(strs[1])]; strs = br.readLine().split("\\s"); for (int i = 0; i < front.length; i++) { front[i] = Integer.parseInt(strs[i]); } strs = br.readLine().split("\\s"); for (int i = 0; i < rear.length; i++) rear[i] = Integer.parseInt(strs[i]); System.out.println(String.format("%.2f", getMax(generateRatio(front, rear)))); } } private LinkedList<Double> generateRatio(int[] front, int[] rear) { LinkedList<Double> list = new LinkedList<Double>(); for (int i = 0; i < front.length; i++) for (int j = 0; j < rear.length; j++) { list.add((double) rear[j] / (double) front[i]); } Collections.sort(list); return list; } private double getMax(LinkedList<Double> list) { double max = 0.0; for (int i = 0; i < list.size() - 1; i++) { max = Math.max(max, list.get(i + 1) / list.get(i)); } return max; } }
No comments :
Post a Comment