Archive for the ‘Topcoder’ Category

SRM 515 DIV 2   Leave a comment


250 FortunateNumbers

import java.util.HashSet;

public class FortunateNumbers {
	public static boolean isFortunate(int n) {
		String s = n + "";
		boolean fortunate = true;
		for (int i = 0; i < s.length() && fortunate; i++) {
			if (s.charAt(i) != '5' && s.charAt(i) != '8')
				fortunate = false;
		}
		return fortunate;
	}
	public static int getFortunate(int[] a, int[] b, int[] c) {
		int count = 0;
		HashSet<Integer> v = new HashSet<Integer>();
		for (int i = 0; i < a.length; i++)
			for (int j = 0; j < b.length; j++)
				for (int k = 0; k < c.length; k++) {
					if (isFortunate(a[i] + b[j] + c[k])
							&& !v.contains(a[i] + b[j] + c[k])) {
						v.add(a[i] + b[j] + c[k]);
						count++;
					}
				}
		return count;
	}
}

500 RotatedClock

public class RotatedClock {
	public static String getEarliest(int hourHand, int minuteHand) {
		for (int i = 0; i < 360; i += 30) {
			int rhh = (hourHand + i) % 360;
			int rmh = (minuteHand + i) % 360;
			if (12 * (rhh % 30) == rmh) {
				int hour = rhh / 30;
				int minute = rmh / 6;
				String HH = hour + "";
				String MM = minute + "";
				if (HH.length() != 2)
					HH = "0" + HH;
				if (MM.length() != 2)
					MM = 0 + MM;
				return HH + ":" + MM;
			}
		}
		return "";
	}
}

Posted August 21, 2011 by epicrado in Topcoder