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;
}
发布了158 篇原创文章 · 获赞 34 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qq_40421671/article/details/95487411
POJ