06/10/2013

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

1 comment:

  1. A computer science student is studying under a tree and another pulls up on a flashy new bike. The first student asks, “Where’d you get that?”

    The student on the bike replies, “While I was studying outside, a beautiful girl pulled up on her bike. She took off all her clothes and said, ‘You can have anything you want’.”

    The first student responds, “Good choice! Her clothes probably wouldn’t have fit you.”

    ReplyDelete