UVA 11547 – Automatic Answer

I just used a double to avoid rounding errors. Then rounded at the end. Pretty easy.

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

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

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) {
double N = sc.nextDouble();
double ans = (((N * 567.) / 9. + 7492) * 235. / 47.) - 498;
ans /= 10;
ans = Math.abs((long) ans);
out.println((long) ans % 10);
T--;
}
out.close();
sc.close();
}
}

UVA 11498 – Division of Nlogonia

I thought this question was going to be a lot more complicated than it turned out.

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

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

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int K = 0;
while ((K = sc.nextInt()) != 0) {
int N = sc.nextInt();
int M = sc.nextInt();
while (K > 0) {
int X = sc.nextInt();
int Y = sc.nextInt();
if (X == N || Y == M) {
out.println("divisa");
} else if (X > N) {
if (Y < M) {
out.println("SE");
} else {
out.println("NE");
}
} else {
if (Y < M) {
out.println("SO");
} else {
out.println("NO");
}
}
K--;
}
}
out.close();
sc.close();
}

}

UVA 11364 – Parking

This one involves a bit of math. You will notice that it doesn’t matter where you park unless you park away from all of the shops.

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

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

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) {
int N = sc.nextInt();
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int i = 0; i < N; i++) {
int next = sc.nextInt();
min = Math.min(min, next);
max = Math.max(max, next);
}
out.println(2 * (max - min));
T--;
}
out.close();
sc.close();
}

}

UVA 11172 – Relational Operator

Really simple. Just a simple if-else.

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

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

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int N = Integer.parseInt(sc.nextLine());
while (N > 0) {
int X = sc.nextInt();
int Y = sc.nextInt();
if (X < Y) {
out.println("<");
} else if (X > Y) {
out.println(">");
} else {
out.println("=");
}
N--;
}
out.close();
sc.close();
}

}

UVA 11044 – Searching for Nessy

I failed by misreading the problem on this one. I thought the sonar beam grid was of size 4 instead of size 3. It cost me two WA’s :(

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

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

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int N = Integer.parseInt(sc.nextLine());
while (N &gt; 0) {
int X = sc.nextInt();
int Y = sc.nextInt();
out.println((long) (Math.ceil((X - 2) * 1.0 / 3) * Math.ceil((Y - 2) * 1.0 / 3)));
N--;
}
out.close();
sc.close();
}

}

UVA 10550 – Combination Lock

A bit of math but an otherwise really simple problem. I would have voted this as harder than the earlier problems as you need to figure out certain numbers based on the direction you are rotating.

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

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

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
while (sc.hasNextLine()) {
int degree = sc.nextInt();
int first = sc.nextInt();
int second = sc.nextInt();
int third = sc.nextInt();
if (first == second && second == third && third == degree && degree == first) {
break;
} else {
out.println(doSolve(degree, first, second, third));
}
}
out.close();
sc.close();
}

private static long doSolve(int degree, int first, int second, int third) {
// 40 numbers = 360 degrees. 1 number = 9 degrees.
long ans = 0;
// 2 clockwise turns.
ans += 720;
// To first number
ans += 9 * findWay(degree, first, 0);
// One counterclockwise turn
ans += 360;
// To second number
ans += 9 * findWay(first, second, 1);
// To third number
ans += 9 * findWay(second, third, 0);
return ans;
}

private static int findWay(int from, int to, int cw) {
//Could memo this technically.

int count = 0;
if (cw == 1) {
while (from != to) {
from++;
if (from == 40)
from = 0;
count++;
}
} else {
while (from != to) {
from--;
if (from == -1)
from = 39;
count++;
}
}
return count;
}
}

UVA 1124 – Celebrity Jeopardy

Just re-print the input. There has to be a cooler way to do this in Java. I really want to just pipe the inputstream to the outputstream somehow.

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

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

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
while (sc.hasNextLine()) {
out.println(sc.nextLine());
}
out.close();
sc.close();
}

}

UVA 272 – TEX Quotes

This problem is super easy beacuse we are guaranteed that every quote is there an even number of times. Just replace the odd and even quotes in a different manner and you are set!

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

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

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
boolean even = true;
while (sc.hasNextLine()) {
char[] line = sc.nextLine().toCharArray();
for (Character c : line) {
if (c == '"') {
if (even) {
out.print("``");
} else {
out.print("''");
}
even = !even;
} else {
out.print(c);
}
}
out.print(System.lineSeparator());
}
out.close();
sc.close();
}

}

Factors

A naive way of figuring out the number of factors of a particular number is to try every number from 1 up to the number itself.

public ArrayList getFactors(int N) {
ArrayList factors = new ArrayList();
for (int i=0; i if (N%i==0) {
}
return factors;
}

We can do much better than this by noticing that for every number i that is a factor of N; the number N/i is also a factor of N! This means that we can change out O(N) algorithm to O(sqrt(N)) which is much much faster.