当前位置:网站首页>7-7 7003 组合锁(PTA程序设计)
7-7 7003 组合锁(PTA程序设计)
2022-07-06 09:22:00 【编程林黛玉】
现在周小小开学了。他们寝室被一密码锁(见图所示)锁住了。他知道密码为三对数,如36-23-12,同时知道开锁的方法。其方法:
(1)先顺时针转两圈。
(2)逆时针转到第一个数的位置。
(3)逆时针转一圈。
(4)顺时针旋转到第二个数。
(5)指针又逆时针转到第三个数。
已知指针的初始位置和密码,问转多少度才能开锁。

输入格式:
有多组数据,每组数据含有四个数,分别是n,fisrt,middle,last,均是小于40(n为起始位置)大于0的。当输入是“0 0 0 0”时,结束。
输出格式:
输出所要转的度数。
输入样例:
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
代码(Python):
d=[] #用来存放结果的列表
n,first,middle,last=map(int,input().split())
def func(first,last): #设置函数,用来计算度数
if first<=last: #分为两种情况,若初始位置较小,则正常减
return (last-first)*9 #9是每个刻度代表的度数
else: #若初始位置较大,说明途中经过了0刻度,就不能直接减了
return (40-first+last)*9 #先用40减去终点刻度,再加上初始刻度就是总刻度,乘以9转换为度数
while 1:
if n==0 and first==0 and middle==0 and last==0: #当输入是“0 0 0 0”时,结束
break
else: #否则输入不是“0 0 0 0”时,则开始计算
res=0 #res是每组数的结果
res+=720 #(1)先顺时针转两圈。
res+=func(first,n) #(2)逆时针转到第一个数的位置。逆时针就用末减初
res+=360 #(3)逆时针转一圈。
res+=func(first,middle) #(4)顺时针旋转到第二个数。顺时针就用初减末
res+=func(last,middle) #(5)指针又逆时针转到第三个数。逆时针就用末减初
d.append(res) #将每组数计算的结果存入列表中,以便一并输出
n,first,middle,last=map(int,input().split()) #继续输入以便进入下一次循环
for i in range(len(d)): #输出结果
print(d[i])我在一开始看到这道题的时候以为它很难,因为看上去很复杂。但还是上手尝试了一下,发现一点也不难,就是最基础的数学知识,也没有很难的算法。所以,当大家遇到一道看上去很难的题的时候,也不要放弃,要尝试入手去做,去分析,说不定它没有很难,又或许它确实有点难度,可是不尝试怎么知道自己会不会呢,万一自己做出来了呢?所以不要给自己设限,要大胆额去尝试,去挑战。
上面的程序给出了比较详细的注释,以便新手小白参考。程序的思路设计或者代码实现并不是最优的,欢迎各位大佬指正错误或者给出更优质的思路。
我是一只想成为鲲鹏的菜鸟,大家的鼓励是我前进的动力,欢迎大家点赞收藏评论哦!
边栏推荐
- 7-6 矩阵的局部极小值(PTA程序设计)
- About the parental delegation mechanism and the process of class loading
- Reinforcement learning series (I): basic principles and concepts
- (ultra detailed onenet TCP protocol access) arduino+esp8266-01s access to the Internet of things platform, upload real-time data collection /tcp transparent transmission (and how to obtain and write L
- Pit avoidance Guide: Thirteen characteristics of garbage NFT project
- [中国近代史] 第五章测验
- 2. C language matrix multiplication
- js判断对象是否是数组的几种方式
- Questions and answers of "signal and system" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
- Leetcode. 3. Longest substring without repeated characters - more than 100% solution
猜你喜欢

一段用蜂鸣器编的音乐(成都)

Questions and answers of "basic experiment" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology

Relationship between hashcode() and equals()

1.C语言矩阵加减法

6. Function recursion

记一次猫舍由外到内的渗透撞库操作提取-flag

Principles, advantages and disadvantages of two persistence mechanisms RDB and AOF of redis

Arduino+ds18b20 temperature sensor (buzzer alarm) +lcd1602 display (IIC drive)

3.输入和输出函数(printf、scanf、getchar和putchar)

Mortal immortal cultivation pointer-1
随机推荐
Implementation of count (*) in MySQL
[modern Chinese history] Chapter V test
Mortal immortal cultivation pointer-2
魏牌:产品叫好声一片,但为何销量还是受挫
1. Preliminary exercises of C language (1)
5月14日杂谈
The latest tank battle 2022 - full development notes-3
简单理解ES6的Promise
JS interview questions (I)
2.C语言矩阵乘法
4. Binary search
受检异常和非受检异常的区别和理解
MySQL锁总结(全面简洁 + 图文详解)
SRC挖掘思路及方法
6.函数的递归
5.MSDN的下载和使用
【黑马早报】上海市监局回应钟薛高烧不化;麦趣尔承认两批次纯牛奶不合格;微信内测一个手机可注册俩号;度小满回应存款变理财产品...
3. Number guessing game
Redis的两种持久化机制RDB和AOF的原理和优缺点
【九阳神功】2020复旦大学应用统计真题+解析