/* * DoesThisMakeMeFat.java * * Created on August 23, 2001, 10:37 AM */ import java.util.*; import java.io.*; /** * * @author schmidt * @version */ public class DoesThisMakeMeFatDriver { /** Creates new DoesThisMakeMeFat */ public DoesThisMakeMeFatDriver() { } public static void main(java.lang.String[] args) { // Read in input from the user until an empty string is passed in. String line = " "; // Used to read in from standard input BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // Will hold each dieter 'group' as the user inputs data Vector units = new Vector(); // Single sorted tree to hold each dieter within a single group TreeSet sortedList = null; // Tokenizer to break apart a line StringTokenizer tokenizer; while (line != null) { // Read in each line. A unit of work begins with 'START' and ends // with 'END' try { line = in.readLine(); } catch(IOException ioErr) { ioErr.printStackTrace(); } if (line != null) { if (line.compareTo("") == 0) { // Disregard blank lines } else if (line.compareTo("START") == 0) { // Start a new unit of work sortedList = new TreeSet(); } else if (line.compareTo("END") == 0) { // Finish this unit of work // -- Add the sorted list into the vector units.add(sortedList); sortedList = null; } else { // Add a new dieter to the list String tok; String sName; int iDays, iWeight; Dieter person; tokenizer = new StringTokenizer(line); // Break apart the string // -- Name tok = tokenizer.nextToken(); sName = tok; // -- Days on diet tok = tokenizer.nextToken(); iDays = new Integer(tok).intValue(); // -- Original Weight tok = tokenizer.nextToken(); iWeight = new Integer(tok).intValue(); // Create the dieter person = new Dieter(sName,iDays,iWeight); // Add the dieter to the sorted list sortedList.add(person); } } } // Finished with the input, output the data for (Enumeration e = units.elements(); e.hasMoreElements();) { Iterator iter; sortedList = (TreeSet)e.nextElement(); iter = sortedList.iterator(); while (iter.hasNext()) { Dieter person = (Dieter)iter.next(); System.out.println(person.getName()); } if (e.hasMoreElements()) { System.out.println(""); } } } } /** holds the information for a single dieter */ class Dieter implements java.util.Comparator, Comparable{ private String sName; private int iWeight; /** Constructor */ public Dieter(String sName, int iDaysOnDiet, int iWeight) { this.sName = sName; this.iWeight = (iWeight - iDaysOnDiet); } // Getter methods public int getWeight() { return iWeight; } public String getName() { return sName; } /** Compare two dieters to see which is "biggest" or heaviest */ public int compare(Object obj1, Object obj2) { Dieter diet1 = (Dieter) obj1; Dieter diet2 = (Dieter) obj2; int result = 0; if (diet1.getWeight() != diet2.getWeight()) { result = (diet2.getWeight() - diet1.getWeight())/Math.abs(diet2.getWeight() - diet1.getWeight()); } else { // The weights are the same, check the name of the dieters int first, second; if (diet1.getName().length() == diet2.getName().length()) { // Check for lexical ordering (If one of the bits is between 0..9 and the other is a..zA..Z, reverse the result for (int loop = 0; loop < diet1.getName().length(); loop++) { byte bits1 = new Byte(diet1.getName().substring(loop,loop+1).getBytes()[0]).byteValue(); byte bits2 = new Byte(diet2.getName().substring(loop,loop+1).getBytes()[0]).byteValue(); if (bits1 != bits2) { result = (bits1 - bits2)/Math.abs(bits1-bits2); } else { result = 0; } if (((bits1 >= 48 && bits1 <= 57) && ((bits2 >= 65 && bits2 <= 90) || (bits2 >= 97 && bits2 <= 122) )) || ((bits2 >= 48 && bits2 <= 57) && ((bits1 >= 65 && bits1 <= 90) || (bits1 >= 97 && bits1 <= 122)))) { result = -result; } if (result != 0) { break; } } } else { result = diet1.getName().compareTo(diet2.getName()); } } return result; } public boolean equals(Object obj) { Dieter diet = (Dieter) obj; return ((this.iWeight == diet.getWeight()) && (this.sName.compareTo(diet.getName()) == 0)); } public int compareTo(java.lang.Object obj) { return compare(this,obj); } }