首页 › 试卷 › Python编程考级 › Python编程四级 › 青少年软件编程(python四级)等级考试试卷-编程题(2024年12月)

青少年软件编程(python四级)等级考试试卷-编程题(2024年12月) VIP

Python编程四级 共 3 题 2024 年 含标准答案与解析
返回试卷列表
本页为整卷浏览:先自己做一遍,再展开「查看答案与解析」核对。

一、简答题

1. 简答题

时钟问题

小明上学时间是8:00,放学时间不固定,他很想知道从上学到放学,时钟的时针、分针和秒针一共有多少次指向了3点钟的位置。他尝试编写了如下程序,请你完善划线处的代码,计算出在h点s分放学时,三个时针分别经过3点位置的次数,用一个元组表示各指针经过3点位置的次数 (时针次数,分针次数,秒针次数) ,并输出。

def fn(h1, m1 ,h2 ,m2 ):   

    if(h2<15):

                ①        

    else:

        hc=1

    th=         ②        

    if(         ③        ):

        mc= th+1

    else:

        mc=th

    sc=        ④        

    return (hc,mc,sc)

h=int(input("h="))

s=int(input("s="))

print(fn(8,0,h,s))

查看答案与解析
答案:<pre class="brush:python;toolbar:false">def fn(h1, m1 ,h2 ,m2 ):        if(h2&lt;15):         hc=0     else:         hc=1     th= h2-h1     if(m2&gt;=15):         mc= th+1     else:         mc=th     sc=th*60+(m2-m1)     return (hc,mc,sc) h=int(input("h=")) s=int(input("s=")) print(fn(8,0,h,s))</pre>
解析:

评分标准:

(1)hc=0 或等效答案;(3分)

(2)h2-h1 或等效答案;(3分)

(3)m2>=15 或等效答案;(3分)

(4)th*60+(m2-m1) 或等效答案。(3分)

2. 简答题

对称字符串

对称字符串是指从左往右读和从右往左读是一样的字符串。小方同学学习了递归思想后,利用递归思想来解决对称问题。在一串包含大小写字符和数字等字符组成的字符串中,他首先筛选出只包含大小写字符组成的字符串,然后判断该字符串是否是对称字符串。他编写的代码如下,请完善划线处的代码。

def getzf(yw):   #筛选出原始字符串yw中大写小写字符组成的字符串

    rst = ''

    for i in yw:

        if 'z'>=i >='a' or 'Z'>=i >='A':

                    ①        

    return rst

def nxzf(data):  #生成字符串data的逆序字符串

    if len(data) == 1:

        return data[0]

    return         ②        


yw =input('请输入字符串')

yw = getzf(yw)

nxzfc = nxzf(yw)

if         ③        :

    print('原字符串中存在对称字符串',yw)

else:

    print('不存在对称字符串')

查看答案与解析
答案:<pre class="brush:python;toolbar:false">def getzf(yw):   #筛选出原始字符串yw中大写小写字符组成的字符串     rst = ''     for i in yw:         if 'z'&gt;=i &gt;='a' or 'Z'&gt;=i &gt;='A':             rst+=i     return rst def nxzf(data):  #生成字符串data的逆序字符串     if len(data) == 1:         return data[0]     return nxzf(data[1:])+data[0] yw =input('请输入字符串') yw = getzf(yw) nxzfc = nxzf(yw) if nxzfc == yw:     print('原字符串中存在对称字符串',yw) else:     print('不存在对称字符串')</pre>
解析:

评分标准:

(1)rst += i 或 rst= rst +i 或等效答案;(2分)

(2)nxzf(data[1:])+data[0] 或等效答案;(3分)

(3)nxzfc == yw 或等效答案。(3分)

3. 简答题

角谷猜想

所谓角谷猜想,是指对于任意一个正整数,如果是奇数,则乘3加1,如果是偶数,则除以2,得到的结果再按照上述规则重复处理,最终总能够得到1。如,假定初始整数为5,计算过程分别为16、8、4、2、1。程序要求输入一个整数,求经过多少次可得到自然数1。 根据题意,请完善划线处的代码。

def fun(n, cnt):

    if         ①        :

        print(cnt)

        return

    else:

        if n % 2 == 0:

            n //=2

                    ②        

        else:

                    ③        

            fun(n, cnt+1)

fun(5,         ④        )

查看答案与解析
答案:<pre class="brush:python;toolbar:false">def fun(n, cnt):     if n == 1:         print(cnt)         return     else:         if n % 2 == 0:             n //=2             fun(n, cnt+1)         else:             n = n * 3 + 1             fun(n, cnt+1) fun(5, 0)</pre>
解析:

评分标准:

(1)n == 1 或等效答案;(3分)

(2)fun(n, cnt+1) 或等效答案;(2分)

(3)n = n * 3 + 1 或等效答案;(3分)

(4)0 或等效答案。(2分)