若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。


  • 运维

  • 前端

  • 编程

  • 随笔

  • hust-oj

1155: Combination Lock

发表于 2017-10-06   |   分类于 HUSTOJ   |   阅读次数 1,237

题目描述

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

参考代码

#include <stdio.h>
int b,i,j,k,n;
long long r,m;
char P[2000], M[10];
dump(int r) 
{
    if (!r) return;
    dump(r/b);
    printf("%d",r%b);
}
main() 
{
    while ((3 == scanf("%d%s%s",&b,P,M)) && b) 
    {
        m = 0;
        for (i=0;M[i];i++) m = m*b + M[i] - '0';
        r = 0;
        for (i=0;P[i];i++) 
        {
            r = r * b + P[i] - '0';
            r %= m;
        }
        if (!r) printf("0");
        dump(r);
        printf("n");
   }
   if(b) printf("missing end delimitern");
}

解析

暂无

hustoj

发表评论 取消回复

电子邮件地址不会被公开。 必填项已用*标注

*
*


hoxis wechat
著作权归作者所有
站点更新说明
  • 文章目录
  • 站点概览
若是凉夜已成梦

若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。

1904 日志
6 分类
12 标签
RSS
weibo github twitter facebook

友情链接

原站点 Skip Dreams孤独患者
© 2017 若是凉夜已成梦
Powered by WordPress | 已运行
Theme By NexT.Mist