原題連結:
http://acm.hust.edu.cn/problem/show/1051
1051 - Combination Lock
Time Limit: 1s Memory Limit: 128MB
Submissions: 156 Solved: 67
- DESCRIPTION
- 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
- 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. OUTPUT
- For each case, print a line with a single integer: the number of degrees that the dial must be turned to open the lock. SAMPLE INPUT
-
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
SAMPLE OUTPUT -
1350 1350 1620 1620 1890 1890
HINT SOURCE
一道水題,就是題意比較難了解。
題意:表盤有40個刻度(0~39),有三個密碼a,b,c,一個初始位置p,解鎖步驟如下,①順時針轉動2圈,②順時針轉到刻度a,③逆時針轉動一圈,④逆時針轉到刻度b,⑤順時針轉到刻度c。要注意的就是轉動的是刻度盤,是以逆時針和順時針不要反了。
代碼:
#include "stdio.h"
int main()
{
int p,a,b,c,ans;
while(scanf("%d%d%d%d",&p,&a,&b,&c)&&(a||b||c||p))
{
ans=40*3+(p+40-a)%40+(b+40-c)%40+(b+40-a)%40;
printf("%d\n",ans*9);
}
return 0;
}