Category Archives: Java

UVA 10300 – Ecological Premium

This was super easy. No need for doubles like I had initially thought.

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

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

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

		int N = sc.nextInt();
		while (N > 0) {
			int f = sc.nextInt();
			long ans = 0;
			for (int i = 0; i < f; i++) {
				int sizeOfFarm = sc.nextInt();
				sc.nextInt();
				int eco = sc.nextInt();
				ans += sizeOfFarm * eco;
			}
			out.println(ans);
			N--;
		}

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

}

UVA 10114 – Loansome Car Buyer

This problem was truly annoying and yet extremely simple at the same time. This has been ever so rightly starred in Competitive Programming 3 book. I messed up with an edge case and I wrote `break` instead of `continue` (big oops) in one of my many submissions. The problem mentions that we need to use files as input and output but that is not the case! That lost me a couple of tries as well :[

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

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

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

		while (true) {
			int duration = sc.nextInt();
			double downpayment = sc.nextDouble();
			double loan = sc.nextDouble();
			int depreciations = sc.nextInt();
			if (duration < 0) {
				break;
			}
			double valOfCar = loan + downpayment;
			double monthlyPayment = loan / duration;

			int[] depList = new int[depreciations];
			double[] depVal = new double[depreciations];
			for (int i = 0; i < depreciations; i++) {
				depList[i] = sc.nextInt();
				depVal[i] = sc.nextDouble();
			}

			int lastMonth = 0;
			double lastVal = depVal[0];

			valOfCar *= (1 - lastVal);

			if (loan < valOfCar) {
				out.println("0 months");
				continue;
			}

			lastMonth = 1;

			for (int i = 1; i < depList.length; i++) {
				int nextMonth = depList[i];
				double nextMonthDepreciation = depVal[i];
				while (lastMonth <= nextMonth) {
					if (nextMonth == lastMonth) {
						lastVal = nextMonthDepreciation;
					}
					valOfCar *= (1 - lastVal);
					loan -= monthlyPayment;
					lastMonth++;
					if (loan < valOfCar) {
						break;
					}
				}
				if (loan < valOfCar) {
					break;
				}
			}
			while (loan > valOfCar) {
				valOfCar *= (1 - lastVal);
				loan -= monthlyPayment;
				lastMonth++;
			}

			lastMonth--;
			if (lastMonth == 1) {
				out.println("1 month");
			} else {
				out.println(lastMonth + " months");
			}
		}
		out.close();
		sc.close();
	}

}

UVA 621 – Secret Research

This was a particularly annoying question. Mainly because the answer choices don’t have an option for ‘Don’t know’. This would ofcourse make the problem much harder (comparitively) but the question allows for inputs such as 190435 which are unclassifiable. I just assumed simple input and my solution worked, so that’s that I suppose.

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

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

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);
    int T = sc.nextInt();
    while (T > 0) {
      String input = sc.next();
      if (input.endsWith("35")) {
    	  out.println("-");
      } else if (input.startsWith("190")) {
    	  out.println("?");
      } else if (input.equals("1") || input.equals("4") || input.equals("78")) {
    	  out.println("+");
      } else {
    	  out.println("*");
      }
      T--;
    }
    out.close();
    sc.close();
  }

}

UVA 12577 – Hajj-e-Akbar

Really, really long question.

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

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

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);
    String line;
    int count = 1;
    while ((line = sc.next()) != null) {
      if (line.trim().equals(&quot;*&quot;))
        break;
      out.print(&quot;Case &quot; + count + &quot;: &quot;);
      if (line.equals(&quot;Hajj&quot;)) {
        out.println(&quot;Hajj-e-Akbar&quot;);
      } else {
        out.println(&quot;Hajj-e-Asghar&quot;);
      }
      count++;
    }
    out.close();
    sc.close();
  }

}

UVA 12403 – Save Setu

Simple .equals() with an if statement.

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

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

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);
    int T = sc.nextInt();
    int count = 0;
    while (T > 0) {
      if (sc.next().equals("donate")) {
        count += sc.nextInt();
      } else {
        out.println(count);
      }
      T--;
    }
    out.close();
    sc.close();
  }

}

UVA 12372 – Packing for Holiday

I tried to use three sc.nextInt()‘s inside the if statement. That failed terribly :(

Super easy problem otherwise.

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

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

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

    int T = sc.nextInt();
    for (int i = 1; i <= T; i++) {
      out.print("Case " + i + ": ");
      int l = sc.nextInt();
      int w = sc.nextInt();
      int h = sc.nextInt();
      if (l <= 20 && w <= 20 && h <= 20) {
        out.println("good");
      } else {
        out.println("bad");
      }
    }

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

}

UVA 12289 – One-Two-Three

Super easy problems indeed. Simple if statements required.

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

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

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);
    int N = sc.nextInt();
    while (N > 0) {
      String input = sc.next();
      if (input.length() == 5) {
        out.println("3");
      } else {
        if ((input.charAt(0) == 'o' && input.charAt(1) == 'n') || (input.charAt(0) == 'o' && input.charAt(2) == 'e') || (input.charAt(1) == 'n' && input.charAt(2) == 'e')) {
          out.println("1");
        } else {
          out.println("2");
        }
      }
      N--;
    }
    out.close();
    sc.close();
  }

}

UVA 12279 – Emoogle Balance

Super simple problem. Positive Numbers – Negative Numbers.

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

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

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

    int count = 1;
    int X = 0;
    while ((X = sc.nextInt()) != 0) {
      int ans = 0;
      for (int i = 0; i < X; i++) {
        if (sc.nextInt() > 0)
          ans++;
        else
          ans--;
      }
      out.println("Case " + count + ": " + ans);
      count++;
    }

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

}

UVA 12250 – Language Detection

I missed the switch statements in Java 7 that can take in Strings as cases for this question. Pretty easy.

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

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

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

    int i = 1;
    String line;
    while ((line = sc.nextLine()) != null) {
      line = line.trim();
      if (line.equals("#"))
        break;
      out.print("Case " + i + ": ");
      if (line.equals("HELLO")) {
        out.println("ENGLISH");
      } else if (line.equals("HOLA")) {
        out.println("SPANISH");
      } else if (line.equals("HALLO")) {
        out.println("GERMAN");
      } else if (line.equals("BONJOUR")) {
        out.println("FRENCH");
      } else if (line.equals("CIAO")) {
        out.println("ITALIAN");
      } else if (line.equals("ZDRAVSTVUJTE")) {
        out.println("RUSSIAN");
      } else {
        out.println("UNKNOWN");
      }
      i++;
    }
    out.close();
    sc.close();
  }
}

UVA 11727 – Cost Cutting

Easiest way is to sort and print the middle number.

import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

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

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);
    int T = sc.nextInt();
    for (int i = 1; i <= T; i++) {
      int[] list = { sc.nextInt(), sc.nextInt(), sc.nextInt() };
      Arrays.sort(list);
      out.println("Case " + i + ": " + list[1]);
    }
    out.close();
    sc.close();
  }

}