2013
11-11

# Combination Lock

Now that you’re back to school for another term, you need to remember how to work the combination lock on your locker. A common design is that of the Master Brand, shown at right. The lock has a dial with 40 calibration marks numbered 0 to 39. A combination consists of 3 of these numbers; for example: 15-25-8. To open the lock, the following steps are taken:
• turn the dial clockwise 2 full turns
• stop at the first number of the combination
• turn the dial counter-clockwise 1 full turn
• continue turning counter-clockwise until the 2nd number is reached
• turn the dial clockwise again until the 3rd number is reached
• pull the shank and the lock will open.

Given the initial position of the dial and the combination for the lock, how many degrees is the dial rotated in total (clockwise plus counter-clockwise) in opening the lock?

Input consists of several test cases. For each case there is a line of input containing 4 numbers between 0 and 39. The first number is the position of the dial. The next three numbers are the combination. Consecutive numbers in the combination will be distinct. A line containing 0 0 0 0 follows the last case.

For each case, print a line with a single integer: the number of degrees that the dial must be turned to open the lock.

0 30 0 30
5 35 5 35
0 20 0 20
7 27 7 27
0 10 0 10
9 19 9 19
0 0 0 0


1350
1350
1620
1620
1890
1890


//* @author 洪晓鹏<hongxp11@163.com>
import java.util.Scanner;

public class Main {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
while (true) {
int begin = in.nextInt();
int one = in.nextInt();
int two = in.nextInt();
int three = in.nextInt();
if ((begin | one | two | three) == 0)
break;
int result = 1080;
// 逆时针
result += begin < one ? (begin + 40 - one) * 9 : (begin - one) * 9;
// 顺时针
result += one < two ? (two - one) * 9 : (40 + two - one) * 9;
// 逆时针
result += two < three ? (two + 40 - three) * 9 : (two - three) * 9;
System.out.println(result);
}
}

}

1. 有一点问题。。后面动态规划的程序中
int dp[n+1][W+1];
会报错 提示表达式必须含有常量值。该怎么修改呢。。