/* Solution to "Arithmetically Challenged." * Straightforward enumeration of all possible values, sorted and then * scanned for longest consecutive sequence. */ import java.util.*; public class Arith { public static Scanner in; public static int caseNum; public static int num[]; public static char op[]; public static HashSet values; public static void main(String[] args) { in = new Scanner(System.in); num = new int[4]; op = new char[]{'+','-','*','/'}; caseNum = 0; while (true) { for (int i = 0; i < 4; i++) num[i] = in.nextInt(); if (num[0] == 0) break; caseNum++; values = new HashSet(); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { for (int k = 0; k < 4; k++) { if (i == j || i == k || j == k) continue; int l = 6 - i - j - k; for (int m = 0; m < 4; m++) { for (int n = 0; n < 4; n++) { for (int o = 0; o < 4; o++) { // FIRST CASE: // ((num[i] op[m] num[j]) op[n] num[k]) op[o] num[l]: int result; if ((result = eval(num[i],op[m],num[j])) != Integer.MIN_VALUE) { if ((result = eval(result,op[n],num[k])) != Integer.MIN_VALUE) { if ((result = eval(result,op[o],num[l])) != Integer.MIN_VALUE) { if (!values.contains(result)) values.add(result); } } } // SECOND CASE: // ((num[i] op[m] num[j]) op[n] (num[k]) op[o] num[l]): if ((result = eval(num[i],op[m],num[j])) != Integer.MIN_VALUE) { int result2; if ((result2 = eval(num[k],op[o],num[l])) != Integer.MIN_VALUE) { if ((result = eval(result,op[n],result2)) != Integer.MIN_VALUE) { if (!values.contains(result)) values.add(result); } } } // THIRD CASE: // (num[i] op[m] (num[j] op[n] (num[k] op[o] num[l])): if ((result = eval(num[k],op[o],num[l])) != Integer.MIN_VALUE) { if ((result = eval(num[j],op[n],result)) != Integer.MIN_VALUE) { if ((result = eval(num[i],op[m],result)) != Integer.MIN_VALUE) { if (!values.contains(result)) values.add(result); } } } // FOURTH CASE: // num[i] op[m] ((num[j] op[n] num[k]) op[o] num[l]): if ((result = eval(num[j],op[n],num[k])) != Integer.MIN_VALUE) { if ((result = eval(result,op[o],num[l])) != Integer.MIN_VALUE) { if ((result = eval(num[i],op[m],result)) != Integer.MIN_VALUE) { if (!values.contains(result)) values.add(result); } } } // FIFTH CASE: // (num[i] op[m] (num[j] op[n] num[k])) op[o] num[l]: if ((result = eval(num[j],op[n],num[k])) != Integer.MIN_VALUE) { if ((result = eval(num[i],op[m],result)) != Integer.MIN_VALUE) { if ((result = eval(result,op[o],num[l])) != Integer.MIN_VALUE) { if (!values.contains(result)) values.add(result); } } } } } } } } } System.out.println("Case " + caseNum + ": " + answer()); } } public static int eval(int a, char oper, int b) { switch(oper) { case '+': return a+b; case '-': return a-b; case '*': return a*b; case '/': if (b == 0 || a%b != 0) return Integer.MIN_VALUE; else return a/b; default: return Integer.MIN_VALUE; } } public static String answer() { Integer vals[] = new Integer[values.size()]; vals = values.toArray(vals); Arrays.sort(vals); int bestlow = 0, besthigh = 0, bestcount = 1; int low = 0, high = 0, count = 1; for (int i = 1; i < vals.length; i++) { if (vals[i] == vals[i-1]+1) { high = i; count++; if (count >= bestcount) { bestlow = low; besthigh = high; bestcount = count; } } else if (bestcount == 1) { low = i; bestlow = low; high = i; besthigh = high; } else { count = 1; low = i; high = i; } } return ""+vals[bestlow]+" to " + vals[besthigh]; } }