06/10/2013

11507 - Bender B. Rodríguez Problem - Java

 package org.goro.uva.m;  
 import java.io.*;  
 import java.util.*;  
 public class BenderTwo {  
      public static int[][] table = {{9, 9, 9, 9, 9, 9},  
                                              {9, 9, 9, 9, 9, 9},  
                                              {2, 3, 1, 0, 4, 5},  
                                              {3, 2, 0, 1, 4, 5},  
                                              {4, 5, 2, 3, 1, 0},  
                                              {5, 4, 2, 3, 0, 1}};  
      public static String[] coder = {"+x", "-x", "+y", "-y", "+z", "-z"};  
      public static void main(String[] args) throws Exception {  
           // FileWriter writer = new FileWriter("output.out");  
           // BufferedWriter out = new BufferedWriter(writer);  
           StringBuilder sb = new StringBuilder();  
           Reader.init(System.in);  
           // Reader.init(new FileInputStream(new File("input.in")));  
           int lenght = Reader.nextInt();  
           while (lenght > 0) {  
                int pos = 0;  
                for (int i = 1; i < lenght; i++) {  
                     String tmp = Reader.next();  
                     if (tmp.charAt(0) != 'N') pos = doStuff(tmp, pos);  
                }  
                sb.append(decode(pos)).append("\n");  
                lenght = Reader.nextInt();  
           }  
           System.out.print(sb);  
           // out.write(sb.toString());  
           // out.close();  
      }  
      public static int encode(char sign, char axis) {  
           if(sign == '+') {  
                if(axis == 'x') return 0;  
                if(axis == 'y') return 2;  
                return 4;  
           }  
           if(axis == 'x') return 1;  
           if(axis == 'y') return 3;  
           return 5;            
      }  
      public static String decode(int num) {  
           return coder[num];  
      }  
      public static int doStuff(String temp, int wire) {  
           int val = encode(temp.charAt(0), temp.charAt(1));  
           return table[val][wire];  
      }  
 }  
 /** 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());  
      }  
 }  

10919 - Prerequisites? - Java

 package org.goro.uva.m;  
 import java.io.*;  
 import java.math.RoundingMode;  
 import java.text.NumberFormat;  
 import java.util.*;  
 public class Prerequisites {  
      public static char[] ch_name1 = new char[25];  
      public static char[] ch_name2 = new char[25];  
      public static void main(String[] args) throws Exception {  
 //          Reader.init(new FileInputStream(new File("input.in")));  
           Reader.init(System.in);  
           StringBuilder sb = new StringBuilder();  
           int noOfCourses = Reader.nextInt();  
           int noOfCat;  
           while (noOfCourses > 0) {  
                noOfCat = Reader.nextInt();  
                int coursesTaken[] = new int[noOfCourses];  
                boolean isValid = true;  
                for(int i = 0; i < noOfCourses; i++) coursesTaken[i] = Reader.nextInt();  
                for(int i = 0; i < noOfCat; i++) {  
                     int tempNoCourses = Reader.nextInt();  
                     int mustHave = Reader.nextInt();  
                     int validated = 0;  
                     for(int j = 0; j < tempNoCourses; j++) {  
                          int tempCurse = Reader.nextInt();  
                          for (int k : coursesTaken) {  
 //                              System.out.println("Checking course: " + tempCurse + " k: " + k);  
                               if (tempCurse == k) {  
                                    validated++;  
                                    break;  
                               }  
                          }  
                     }  
                     if(validated < mustHave) {  
                          isValid = false;  
 //                         System.out.println("Valid: " + validated + " must have: " + mustHave);  
                     }  
                }  
                if(isValid) sb.append("yes\n");  
                else sb.append("no\n");  
                noOfCourses = Reader.nextInt();  
           }  
           System.out.print(sb);  
      }  
 }  
 /** 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());  
      }  
 }  

10424 - Love Calculator - Java

 package org.goro.uva.m;  
 import java.io.*;  
 import java.math.RoundingMode;  
 import java.text.NumberFormat;  
 import java.util.*;  
 public class LoveCalculator {  
      public static char[] ch_name1 = new char[25];  
      public static char[] ch_name2 = new char[25];  
      public static void main(String[] args) throws Exception {  
 //          InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("input.in")));  
           InputStreamReader isr = new InputStreamReader(System.in);  
           BufferedReader br = new BufferedReader(isr);  
           StringBuilder sb = new StringBuilder("");  
           String name1;  
           String name2;  
           while ((name1 = br.readLine()) != null) {  
                name2 = br.readLine();  
                name1 = name1.toLowerCase();  
                name2 = name2.toLowerCase();  
                ch_name1 = name1.toCharArray();  
                ch_name2 = name2.toCharArray();  
                int val1 = calculate(ch_name1);  
                int val2 = calculate(ch_name2);  
                if(val1 < val2) {  
                     val1 ^= val2;  
                     val2 ^= val1;  
                     val1 ^= val2;  
                }  
                float propo = 0;  
                if( val1 != 0 ) propo = (float)val2 / (float)val1;  
                propo *=100;  
                sb.append(round(propo, 2)).append(" %\n");  
           }  
           System.out.print(sb);  
      }  
      static public String round(double d, int ic) {  
            NumberFormat nf = NumberFormat.getInstance();  
            nf.setMaximumFractionDigits(ic);  
            nf.setMinimumFractionDigits(ic);  
            double tmp = Double.parseDouble((nf.format(d)).replaceAll(",", ".").replaceAll(" ", "") );  
            String temp = Double.toString(tmp);  
            if(temp.endsWith(".0")) return temp + "0";  
            else return temp;  
      }  
      public static int calculate(char[] arr) {  
           int retVal = 0;  
           for(int i = 0; i < arr.length; i++) {  
                if(arr[i] >= 'a' && arr[i] <= 'z') retVal += arr[i] - 'a' + 1;  
           }  
           return add(retVal);  
 //          return retVal;  
      }  
      public static int add(int val) {  
 //          System.out.println("running: " + val);  
           String tmp = Integer.toString(val);  
 //          System.out.println("String: " + tmp + "lenght:" + tmp.length());  
           int retVal = 0;  
           for(int i = 0; i < tmp.length(); i++) {  
                retVal += (tmp.charAt(i) - '0');  
 //               System.out.println(tmp.charAt(i));  
           }  
 //          System.out.println("got: " + retVal);  
           if(retVal > 9) return add(retVal);  
           else return retVal;  
      }  
 }  

10324 - Zeros and Ones - Java

 package org.goro.uva.m;  
 import java.io.*;  
 import java.util.*;  
 public class ZerosAndOnes {  
      public static void main(String[] args) throws Exception {  
           InputStreamReader isr = new InputStreamReader(System.in);  
           BufferedReader br = new BufferedReader(isr);  
           StringBuilder sb = new StringBuilder("");  
           int testCase = 1;  
           String line;  
           while ((line = br.readLine()) != null) {  
                sb.append("Case ").append(testCase).append(":\n");  
                testCase++;  
                int noOfCases = Integer.parseInt(br.readLine());  
                for (int j = 0; j < noOfCases; j++) {  
                     String[] str = br.readLine().split(" ");  
                     int val1 = Integer.parseInt(str[0]);  
                     int val2 = Integer.parseInt(str[1]);  
                     if (val1 > val2) {  
                          val1 ^= val2;  
                          val2 ^= val1;  
                          val1 ^= val2;  
                     }  
                     boolean isValid = true;  
                     if (val1 != val2) {  
                          for (int i = val1; i < val2; i++) {  
                               if (line.charAt(i) != line.charAt(i + 1)) {  
                                    isValid = false;  
                                    break;  
                               }  
                          }  
                     }  
                     if (isValid)  
                          sb.append("Yes\n");  
                     else   
                          sb.append("No\n");            
                }  
           }  
           System.out.print(sb);  
      }  
 }  

15/08/2013

10141 - Request for Proposal - Java

import java.io.*;  
 import java.util.*;  
 public class Request {  
      public static void main(String[] args) throws Exception {  
           Reader.init(System.in);  
           int noOfRequirements = Reader.nextInt();  
           int noOfProposals = Reader.nextInt();  
           double minPrice;  
           int maxProposals;  
           int bestOffer;  
           for(int k = 1; noOfRequirements > 0; k++) {  
                String[] proposalName = new String[noOfProposals];  
                double[] proposalPrice = new double[noOfProposals];  
                int[] metReqs = new int[noOfProposals];  
                for (int i = 0; i < noOfRequirements; i++) {  
                     Reader.reader.readLine();  
                }  
                minPrice = 0;  
                maxProposals = -1;  
                bestOffer = 0;  
                for (int i = 0; i < noOfProposals; i++) {  
                     proposalName[i] = Reader.reader.readLine();  
                     proposalPrice[i] = Reader.nextDouble();  
                     metReqs[i] = Reader.nextInt();  
                     for (int j = 0; j < metReqs[i]; j++) {  
                          Reader.reader.readLine();  
                     }  
                     if (metReqs[i] > maxProposals) {  
                          maxProposals = metReqs[i];  
                          minPrice = proposalPrice[i];  
                          bestOffer = i;  
                     } else if (metReqs[i] == maxProposals && minPrice > proposalPrice[i]) {  
                               minPrice = proposalPrice[i];  
                               bestOffer = i;  
                     }  
                }  
                System.out.println("RFP #" + k);  
                System.out.println(proposalName[bestOffer]);  
                noOfRequirements = Reader.nextInt();  
                noOfProposals = Reader.nextInt();  
                if(noOfRequirements != 0) System.out.println("");  
           }  
      }  
 }  
 /** 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());  
      }  
 }  

661 - Blowing Fuses - Java

import java.io.*;  
 import java.util.*;  
 public class BlowingFuses {  
      public static void main(String[] args) throws Exception {  
 //          FileWriter writer = new FileWriter("output.out");  
 //          BufferedWriter out = new BufferedWriter(writer);  
           Reader.init(System.in);  
 //          Reader.init(new FileInputStream(new File("input.in")));  
           int currentCase = 1;  
           int noOfDevices = Reader.nextInt();  
           int noOfOperations;  
           int capacity;  
           int currentConsumption;  
           int maxConsumption;  
           boolean[] devicesState;  
           int[] devicesConsumption;  
           int tempDevice;  
           boolean isDone;  
           while (noOfDevices != 0) {  
                System.out.println("Sequence " + currentCase);  
                currentConsumption = 0;  
                maxConsumption = 0;  
                noOfOperations = Reader.nextInt();  
                capacity = Reader.nextInt();  
                devicesState = new boolean[noOfDevices];  
                devicesConsumption = new int[noOfDevices];  
                isDone = false;  
                for(int i = 0; i < noOfDevices; i++) {  
                     devicesConsumption[i] = Reader.nextInt();  
                     devicesState[i] = false;  
                }  
                for(int i = 0; i < noOfOperations; i++) {  
                     tempDevice = Reader.nextInt() - 1;  
                     if(!devicesState[tempDevice]) {  
                          //power on...  
                          devicesState[tempDevice] = true;  
                          currentConsumption += devicesConsumption[tempDevice];  
                          if(currentConsumption > capacity) {  
                               for(i = i + 1; i < noOfOperations; i++) Reader.nextInt();  
                               System.out.println("Fuse was blown.\n");  
                               isDone = true;  
                          }  
                          if(currentConsumption > maxConsumption) maxConsumption = currentConsumption;  
                     } else {  
                          //shutting down...  
                          currentConsumption -= devicesConsumption[tempDevice];  
                          devicesState[tempDevice] = false;  
                     }  
                }  
                if(!isDone) {  
                     System.out.println("Fuse was not blown.");  
                     System.out.println("Maximal power consumption was " + maxConsumption + " amperes.\n");  
                }  
                noOfDevices = Reader.nextInt();  
                currentCase++;  
           }  
 //          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());  
      }  
 }  

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());  
      }  
 }