当前位置:网站首页>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.数组、指针和数组的关系
- fianl、finally、finalize三者的区别
- 编写程序,模拟现实生活中的交通信号灯。
- 6. Function recursion
- The difference between cookies and sessions
- [modern Chinese history] Chapter V test
- 【九阳神功】2022复旦大学应用统计真题+解析
- [modern Chinese history] Chapter 6 test
- 一段用蜂鸣器编的音乐(成都)
- 20220211-CTF-MISC-006-pure_ Color (use of stegsolve tool) -007 Aesop_ Secret (AES decryption)
猜你喜欢
5.MSDN的下载和使用
Difference and understanding between detected and non detected anomalies
hashCode()与equals()之间的关系
Cookie和Session的区别
5. Function recursion exercise
Write a program to simulate the traffic lights in real life.
Service ability of Hongmeng harmonyos learning notes to realize cross end communication
[面試時]——我如何講清楚TCP實現可靠傳輸的機制
C language Getting Started Guide
2. Preliminary exercises of C language (2)
随机推荐
【九阳神功】2016复旦大学应用统计真题+解析
Miscellaneous talk on May 27
Mortal immortal cultivation pointer-2
C语言入门指南
抽象类和接口的区别
PriorityQueue (large root heap / small root heap /topk problem)
4.二分查找
4.分支语句和循环语句
7-6 矩阵的局部极小值(PTA程序设计)
5.函数递归练习
ArrayList的自动扩容机制实现原理
[the Nine Yang Manual] 2021 Fudan University Applied Statistics real problem + analysis
Safe driving skills on ice and snow roads
Change vs theme and set background picture
vector
FAQs and answers to the imitation Niuke technology blog project (III)
String ABC = new string ("ABC"), how many objects are created
6.函数的递归
View UI plus releases version 1.1.0, supports SSR, supports nuxt, and adds TS declaration files
MySQL lock summary (comprehensive and concise + graphic explanation)