Description

- 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
解这道题的关键在于题意的理解,要注意的是,转动的不是表的指针,而是外面的刻度。
代码如下:
#include<iostream>
using namespace std;
int main()
{
int p0,p1,p2,p3,t1,t2,t3,res;
cin>>p0>>p1>>p2>>p3;
while(1)
{
if(p0==0&&p1==0&&p2==0&&p3==0)
break;
if(p0<p1)
t1=9*(40+p0-p1);
else if(p0>=p1)
t1=9*(p0-p1);
if(p1>p2)
t2=9*(40+p2-p1);
else if(p1<=p2)
t2=9*(p2-p1);
if(p2<p3)
t3=9*(40+p2-p3);
else if(p2>=p3)
t3=9*(p2-p3);
res=1080+t1+t2+t3;
cout<<res<<endl;
cin>>p0>>p1>>p2>>p3;
}
return 0;
}
总结:在做这道题的时候,思路还是不够清晰,wrong了3次,说明自己的逻辑思维能力还很欠缺,唉,
谁叫我以前没好好学数学的啊,今后不能再忽视数学了。