読者です 読者をやめる 読者になる 読者になる

TopCoder SRM 522 Div1 Medium: CorrectMultiplication

コード

public class CorrectMultiplication {

	public long getMinimum(int a, int b, int c) {
		if (a > b) {
			int bb = b;
			b = a;
			a = bb;
		}

		long ans = Long.MAX_VALUE;
		for (int A = 1; A < 1000000; A++) {
			long B = c / A;
			for (int d = -5; d <= 5; d++) {
				long C = (long) A * (B + d);

				if (C == 0) {
					continue;
				}

				long da = Math.abs(A - a);
				long db = Math.abs(B + d - b);
				long dc = Math.abs(C - c);

				ans = Math.min(da + db + dc, ans);
			}
		}
		return ans;
	}
}