当前位置:网站首页>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])
我在一开始看到这道题的时候以为它很难,因为看上去很复杂。但还是上手尝试了一下,发现一点也不难,就是最基础的数学知识,也没有很难的算法。所以,当大家遇到一道看上去很难的题的时候,也不要放弃,要尝试入手去做,去分析,说不定它没有很难,又或许它确实有点难度,可是不尝试怎么知道自己会不会呢,万一自己做出来了呢?所以不要给自己设限,要大胆额去尝试,去挑战。
上面的程序给出了比较详细的注释,以便新手小白参考。程序的思路设计或者代码实现并不是最优的,欢迎各位大佬指正错误或者给出更优质的思路。
我是一只想成为鲲鹏的菜鸟,大家的鼓励是我前进的动力,欢迎大家点赞收藏评论哦!
边栏推荐
- 记一次猫舍由外到内的渗透撞库操作提取-flag
- ABA问题遇到过吗,详细说以下,如何避免ABA问题
- The latest tank battle 2022 full development notes-1
- FAQs and answers to the imitation Niuke technology blog project (I)
- [中国近代史] 第五章测验
- Detailed explanation of redis' distributed lock principle
- 5. Download and use of MSDN
- Thoroughly understand LRU algorithm - explain 146 questions in detail and eliminate LRU cache in redis
- Redis cache obsolescence strategy
- Using qcommonstyle to draw custom form parts
猜你喜欢
C language to achieve mine sweeping game (full version)
Custom RPC project - frequently asked questions and explanations (Registration Center)
MySQL事务及实现原理全面总结,再也不用担心面试
View UI plus released version 1.3.0, adding space and $imagepreview components
4.二分查找
透彻理解LRU算法——详解力扣146题及Redis中LRU缓存淘汰
(original) make an electronic clock with LCD1602 display to display the current time on the LCD. The display format is "hour: minute: Second: second". There are four function keys K1 ~ K4, and the fun
The difference between cookies and sessions
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
The latest tank battle 2022 - Notes on the whole development -2
随机推荐
Implement queue with stack
仿牛客技术博客项目常见问题及解答(二)
7. Relationship between array, pointer and array
5.函数递归练习
Caching mechanism of leveldb
Thoroughly understand LRU algorithm - explain 146 questions in detail and eliminate LRU cache in redis
Differences among fianl, finally, and finalize
[the Nine Yang Manual] 2020 Fudan University Applied Statistics real problem + analysis
Write a program to simulate the traffic lights in real life.
3. C language uses algebraic cofactor to calculate determinant
Redis cache obsolescence strategy
优先队列PriorityQueue (大根堆/小根堆/TopK问题)
Using spacedesk to realize any device in the LAN as a computer expansion screen
[modern Chinese history] Chapter V test
ABA问题遇到过吗,详细说以下,如何避免ABA问题
3.猜数字游戏
Miscellaneous talk on May 14
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
渗透测试学习与实战阶段分析
String ABC = new string ("ABC"), how many objects are created