import java.util.*; public class CombinationLock { static final int DEGREE = 9 ; // (360 /40 = 9) public static void main(String[] args){ String input = "1350 30 0 30\n" + "1620 20 0 20\n"+ "1620 27 7 27\n"+ "1890 10 0 10\n"+ "1197 11 19 15\n" + "1620 10 30 10"; Scanner s = new Scanner(input); while(s.hasNext()){ int total = s.nextInt(); int cmb1 = s.nextInt(); int cmb2 = s.nextInt(); int cmb3 = s.nextInt(); int pos = process(cmb1,cmb2,cmb3,total); System.out.println(pos); } } /* * this method compute the total of degrees when rotating the lock. * @param initial position of lock, comb1, comb2, and comb3 * @return the total degrees of rotation */ public static int computeTotal(int initial, int cmb1, int cmb2, int cmb3){ int total = 360 * 2; // initial --> combination1 (clock wise) int s = 0; if(initial >= cmb1){ s= ( initial - cmb1) * 9; }else{ s = ( 40 - (cmb1- initial)) * 9; } total += s; //combination 1 --> combination2 (counter clock wise) total += 360; if(cmb1 >= cmb2){ s= ( 40 - Math.abs(cmb2- cmb1)) * 9; }else{ s= Math.abs( cmb1 - cmb2) * 9; } total+= s; //combination 2 --> combination3 (clock wise) if(cmb2 >= cmb3){ s= ( cmb2 - cmb3) * 9; }else{ s= ( 40 - (cmb3- cmb2)) * 9; } total+=s; return total; } public static int process(int cmb1, int cmb2, int cmb3, int total){ total -= (3 * 360) ; total /=9; int sub = ( 40 - cmb3 + cmb2) % 40 ; total -= sub; sub = (40 - cmb1 + cmb2) % 40; total -= sub; return (total + cmb1) % 40; } }