当前位置:网站首页>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])
我在一开始看到这道题的时候以为它很难,因为看上去很复杂。但还是上手尝试了一下,发现一点也不难,就是最基础的数学知识,也没有很难的算法。所以,当大家遇到一道看上去很难的题的时候,也不要放弃,要尝试入手去做,去分析,说不定它没有很难,又或许它确实有点难度,可是不尝试怎么知道自己会不会呢,万一自己做出来了呢?所以不要给自己设限,要大胆额去尝试,去挑战。
上面的程序给出了比较详细的注释,以便新手小白参考。程序的思路设计或者代码实现并不是最优的,欢迎各位大佬指正错误或者给出更优质的思路。
我是一只想成为鲲鹏的菜鸟,大家的鼓励是我前进的动力,欢迎大家点赞收藏评论哦!
边栏推荐
- Caching mechanism of leveldb
- Have you encountered ABA problems? Let's talk about the following in detail, how to avoid ABA problems
- Beautified table style
- [the Nine Yang Manual] 2017 Fudan University Applied Statistics real problem + analysis
- [the Nine Yang Manual] 2020 Fudan University Applied Statistics real problem + analysis
- 1. C language matrix addition and subtraction method
- A piece of music composed by buzzer (Chengdu)
- String ABC = new string ("ABC"), how many objects are created
- 更改VS主题及设置背景图片
- 【九阳神功】2019复旦大学应用统计真题+解析
猜你喜欢
Questions and answers of "Fundamentals of RF circuits" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
1. C language matrix addition and subtraction method
1.C语言矩阵加减法
[during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission
Questions and answers of "basic experiment" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
9. Pointer (upper)
(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
3.猜数字游戏
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
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
随机推荐
Detailed explanation of redis' distributed lock principle
8.C语言——位操作符与位移操作符
[during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission
JS interview questions (I)
C语言入门指南
1. First knowledge of C language (1)
There is always one of the eight computer operations that you can't learn programming
The latest tank battle 2022 full development notes-1
Miscellaneous talk on May 27
[the Nine Yang Manual] 2016 Fudan University Applied Statistics real problem + analysis
Mode 1 two-way serial communication is adopted between machine a and machine B, and the specific requirements are as follows: (1) the K1 key of machine a can control the ledi of machine B to turn on a
Custom RPC project - frequently asked questions and explanations (Registration Center)
C language Getting Started Guide
C语言入门指南
Redis cache obsolescence strategy
Service ability of Hongmeng harmonyos learning notes to realize cross end communication
[the Nine Yang Manual] 2019 Fudan University Applied Statistics real problem + analysis
canvas基础1 - 画直线(通俗易懂)
【九阳神功】2022复旦大学应用统计真题+解析
(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