POJ 2304

Combination Lock

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4966   Accepted: 3035

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

Source

Waterloo local 2003.09.20

大致題意:

模擬一個開組合的密碼鎖過程。就像電影你開保險箱一樣,左轉幾圈右轉幾圈的就搞定了。這個牌子的鎖呢,也有它獨特的轉法。這個鎖呢,有一個轉盤,刻度爲0~39。在正北方向上有一個刻度指針。它的密碼組合有三個數,開鎖的套路爲:先把刻度盤順時針轉兩圈,然後再順時針轉到第一個數,再把刻度盤逆時針轉一圈,再逆時針轉到第二個數,最後再順時針轉到第三個數。這裏的轉到那個數是指將刻度盤上的數轉到指針處。起始位置和組合密碼有標準輸入給出。求圓盤轉過的總度數(順時針加上逆時針)。注意刻度盤上還有一個凸起的圓盤,這個是不能轉的。

解題思路:

思路:不是轉錶針,是轉錶盤,真好和轉錶針相反

給個邏輯代碼,st是起始的地方,ed是終止的地方

//順時針
if(ed > st)
    sum += (40 - ed + st) * 9;
else
    sum += (st - ed) * 9;
//逆時針
if(ed > st)
    sum += (ed - st) * 9;
else
    sum += (40 - st + ed) * 9;

代碼:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long LL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int main()
{
    int st,a,b,c;
    while(scanf("%d%d%d%d",&st,&a,&b,&c))
    {
        if(st + a + b + c == 0)
            break;
        int sum = 1080;
        if(a > st)
            sum += (40 - a + st) * 9;
        else
            sum += (st - a) * 9;
        if(b > a)
            sum += (b - a) * 9;
        else
            sum += (40 - a + b) * 9;
        if(c > b)
            sum += (40 - c + b) * 9;
        else
            sum += (b - c) * 9;
        printf("%d\n",sum);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章