11/08/2013

573 - The Snail - Java

 package org.goro.uva.m;  
 import java.io.*;  
 import java.util.*;  
 public class TheSnail {  
      public static void main(String[] args) throws Exception {  
 //          FileWriter writer = new FileWriter("output.out");  
 //          BufferedWriter out = new BufferedWriter(writer);  
           try {  
                Reader.init(System.in);  
 //               Reader.init(new FileInputStream(new File("input.in")));  
                while (true) {  
                     int height = Reader.nextInt();  
                     if(height == 0) break;  
                     int dayDistance = Reader.nextInt();  
                     int slidingDistance = Reader.nextInt();  
                     int fatigueFactor = Reader.nextInt();  
                     int dayLoss = fatigueFactor * dayDistance;  
                     height *= 100;  
                     dayDistance *= 100;  
                     slidingDistance *= 100;  
 //                    out.write("DEBUG: New test case: H:" + height + " U:" + dayDistance + " D:" + slidingDistance + " F:" + fatigueFactor + " Loss:" + dayLoss);  
                     int currentDay = 1;  
                     int currentHeight = 0;  
                     boolean isRunning = true;  
                     while(isRunning) {  
                          if(dayDistance > 0) {  
                               currentHeight += dayDistance;  
                               dayDistance -= dayLoss;  
                          }  
 //                         out.write("DEBUG: Day:" + currentDay + " currHeight:" + currentHeight + "/" + height + " daySpeed: " + dayDistance + "\n");  
                          if (currentHeight > height) {  
                               System.out.println("success on day " + currentDay);  
 //                              out.write("success on day " + currentDay + "\n");  
                               isRunning = false;  
                          }  
                          currentHeight -= slidingDistance;  
                          if(currentHeight < 0 && isRunning) {  
                               System.out.println("failure on day " + currentDay);  
 //                              out.write("failure on day " + currentDay + "\n");  
                               isRunning = false;  
                          }  
                          currentDay++;  
                     }  
                }  
           } catch (Exception ex) {  
                // ex.printStackTrace();  
           }  
 //          out.close();  
      }  
 }  
 /** Class for buffered reading int and double values */  
 /** http://www.cpe.ku.ac.th/~jim/java-io.html */  
 class Reader {  
      static BufferedReader reader;  
      static StringTokenizer tokenizer;  
      /** call this method to initialize reader for InputStream */  
      static void init(InputStream input) {  
           reader = new BufferedReader(new InputStreamReader(input));  
           tokenizer = new StringTokenizer("");  
      }  
      /** get next word */  
      static String next() throws IOException {  
           while (!tokenizer.hasMoreTokens()) {  
                tokenizer = new StringTokenizer(reader.readLine());  
           }  
           return tokenizer.nextToken();  
      }  
      static int nextInt() throws IOException {  
           return Integer.parseInt(next());  
      }  
      static double nextDouble() throws IOException {  
           return Double.parseDouble(next());  
      }  
 }  

2 comments:

  1. I called the janitor the other day to see what he could do about my dingy linoleum floor. He said he would have been happy to loan me a polisher, but that he hadn't the slightest idea what he had done with it. I told him not to worry about it - that as a programmer it wasn't the first time I had experienced a buffer allocation failure due to a memory error.

    ReplyDelete

  2. These two strings walk into a bar and sit down. The bartender says, "So what'll it be?"

    The first string says, "I think I'll have a beer quag fulk boorg jdk^CjfdLk jk3s d#f67howe%^U r89nvy owmc63^Dz x.xvcu"

    "Please excuse my friend," the second string says, "He isn't null-terminated."

    ReplyDelete