UVA 10424 – Love Calculator

Pretty easy. Just make appropriate functions and call them recursively.

import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Scanner;

/**
 * 
 * @author Sanchit M. Bhatnagar
 * @see http://uhunt.felix-halim.net/id/74004
 * 
 */
public class P10424 {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);

    DecimalFormat df = new DecimalFormat();
    df.setMinimumFractionDigits(2);
    df.setMaximumFractionDigits(2);

    while (sc.hasNextLine()) {
      String first = sc.nextLine().toLowerCase();
      String second = sc.nextLine().toLowerCase();
      double numFirst = toNum(convert(first));
      double numSecond = toNum(convert(second));
      double ans;
      if (numFirst < numSecond) {
        ans = numFirst * 100 / numSecond;
      } else {
        ans = numSecond * 100 / numFirst;
      }
      out.println(df.format(ans) + " %");
    }

    out.close();
    sc.close();
  }

  private static String convert(String first) {
    int x = 0;
    for (int i = 0; i < first.length(); i++) {
      char c = first.charAt(i);
      if (c >= 'a' && c <= 'z')
        x += (c - 'a') + 1;
    }
    return x + "";
  }

  private static double toNum(String first) {
    if (first.length() != 1) {
      int x = 0;
      for (int i = 0; i < first.length(); i++) {
        x += Integer.parseInt(first.charAt(i) + "");
      }
      return toNum(x + "");
    } else {
      return Double.parseDouble(first);
    }
  }

}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.