buctoj-python 2022.5.26

A 侯先生爬楼梯

题目描述

侯先生每天都会爬楼梯锻炼身体,他爬楼梯的上跨步长有且仅有两种,或者一次上跨一级,或者一次上跨两级。‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬
有一天侯先生想弄明白一个很难的问题:从最下面的平台开始到顶端的第n级一共有多少种走法呢?‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬
例如n是2时,有两种走法:直接从平台上跨两步到第2级,或者从平台跨一步到1级再跨一步到第2级。‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬
请你帮帮侯先生,给你n(1<=n<40)的值,你帮忙计算并输出有多少种爬到顶端的方法。

输入

输入n的值,n是1到40之间的整数。

输出

输出一共有多少种从平台到第n级台阶的走法。

样例输入 复制

3

样例输出 复制

3

cnt[i]=cnt[i-1]+cnt[i-2]

n=int(input())
cnt=[]
cnt.append(0)
cnt.append(1)
cnt.append(2)
for i in range(3,n+1):
    cnt.append(cnt[i-1]+cnt[i-2])
print(cnt[n])

B 二分法求平方根

题目描述

设计一个用二分法计算一个大于或等于 1 的实数 n
的平方根的函数sqrt_binary(n),计算精度控制在计算结果的平方与输入的误差不大于1e-6。‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬
注:初始区间取[0,n]

输入

输入一个实数 n(大于或等于1)

输出

第一行输出用自己设计的函数计算得到的平方根‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬
第二行输出用math库开平方函数计算得到的平方根

样例输入 复制

5.0

样例输出 复制

2.2360679507255554
2.23606797749979
import math
n=eval(input())
l=0
r=n
mid=(l+r)/2
while abs(mid*mid-n)>1e-6:
    mid=(l+r)/2
    if mid*mid>n:
        r=mid
    else:
        l=mid
print(l)
print(math.sqrt(n))

C 子列表最大长度

题目描述

递归检查一个列表的所有子列表及各层次子列表的子列表的长度,返回所有子列表中最大长度。

样例输入 复制

[4, 1, 3, 11, [1, 6, 8, [1, 2, 3, 4, 5, 6, 7]], [[1, 3], [6, 15]]]

样例输出 复制

7

写一个递归函数,注意使用全局变量

max=0
def recursion(lst):
    length=len(lst)
    global max
    if length>max:
        max=length
    for i in lst:
        if type(i)==list:
            recursion(i)
 
li=eval(input())
recursion(li)
print(max)

D 列表的合并与排序

题目描述

读入两行,两行的格式一样,都是用空格分隔的若干个整数,将这些数合并到一个列表中,降序排列后输出整个列表。‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬
提示: list1 = list(map(int,input().split())) #读入一行由空格分隔的整数,将其存入list1列表中

输入

输入为两行,两行格式一样,都是用空格分隔的若干个整数(整数个数可能为0个)。

输出

输出为一行,是元素按降序排列后的整数列表。

样例输入 复制

1 5 9 -1 0
234 6 9 2 34 0

样例输出 复制

[234, 34, 9, 9, 6, 5, 2, 1, 0, 0, -1]
l1=list(map(int,input().split())) #map() 会根据提供的函数对指定序列做映射。
l2=list(map(int,input().split()))
l1+=l2
for i in range(len(l1)): #冒泡排序
    for j in range(len(l1)-1):
        if l1[j+1]>l1[j]:
            l1[j],l1[j+1]=l1[j+1],l1[j] #交换
print(l1)

E 统计字母数量

题目描述

有如下一段英文短文,请编写程序统计这段短文前 n 小段中每一个英文字母出现的次数,结果按次数降序排列,次数相同时,按字母表顺序输出。若 n
值大于短文行数,输出整篇文章中每一个英文字母出现的次数(大写字母按小写字母统计)。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

输入

输入一个正整数
n‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬ 短文
end

输出

分行输出每个字母的数量,数量占3个字符宽度,居右对齐(参考示例输出)

‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬
样例输入 复制

2
He was an old man who fished alone in a skiff in the Gulf Stream and he had gone eighty-four days now without taking a fish. In the first forty days a boy had been with him. But after forty days without a fish the boy’s parents had told him that the old man was now definitely and finally salao, which is the worst form of unlucky, and the boy had gone at their orders in another boat which caught three good fish the first week. It made the boy sad to see the old man come in each day with his skiff empty and he always went down to help him carry either the coiled lines or the gaff and harpoon and the sail that was furled around the mast. The sail was patched with flour sacks and, furled, it looked like the flag of permanent defeat.
The old man was thin and gaunt with deep wrinkles in the back of his neck. The brown blotches of the benevolent skin cancer the sun brings from its reflection on the tropic sea were on his cheeks. The blotches ran well down the sides of his face and his hands had the deep-creased scars from handling heavy fish on the cords. But none of these scars were fresh. They were as old as erosions in a fishless desert.
Everything about him was old except his eyes and they were the same color as the sea and were cheerful and undefeated.
“Santiago,“ the boy said to him as they climbed the bank from where the skiff was hauled up. “I could go with you again. We’ve made some money.“
The old man had taught the boy to fish and the boy loved him.
“No,“ the old man said. “You’re with a lucky boat. Stay with them.“
“But remember how you went eighty-seven days without fish and then we caught big ones every day for three weeks.“
“I remember,“ the old man said. “I know you did not leave me because you doubted.“
“It was papa made me leave. I am a boy and I must obey him.“
“I know,“ the old man said. “It is quite normal.“
“He hasn’t much faith.“
end

样例输出 复制

e 的数量是 106 个
h 的数量是  80 个
t 的数量是  79 个
a 的数量是  78 个
s 的数量是  66 个
n 的数量是  63 个
o 的数量是  63 个
i 的数量是  58 个
d 的数量是  47 个
r 的数量是  44 个
f 的数量是  38 个
l 的数量是  34 个
w 的数量是  28 个
c 的数量是  24 个
y 的数量是  19 个
m 的数量是  16 个
u 的数量是  15 个
b 的数量是  14 个
k 的数量是  13 个
g 的数量是  12 个
p 的数量是   9 个
v 的数量是   2 个
j 的数量是   0 个
q 的数量是   0 个
x 的数量是   0 个
z 的数量是   0
n=int(input())
dic={}
i=0
while(1):
    s=input()
    i+=1
    if s=='end':
        break
    if(i<=n):
        for j in s:
            if 'A'<=j<='Z': #转为小写
                j=ord(j)-ord('A')+ord('a')
                j=chr(j)
            if 'a'<=j<='z':
                if dic.get(j,0)!=0:
                    dic[j]+=1
                else:
                    dic[j]=1
for i in range(26):
    j=chr(i+ord('a'))
    if dic.get(j,0)==0: #将未出现的字母赋值为0
        dic[j]=0
new_dic=sorted(dic.items(),key= lambda x:(x[1],-ord(x[0])),reverse=True)
#排序,按照值降序排,如果值相同,按键升序排
for i in new_dic:
    print("%s 的数量是 %3d 个"%(i[0],i[1])) #%3d输出3位,默认右对齐

F 校验地区身份证号码并输出全部信息

题目描述

中国目前采用的是18位身份证号,其第1-2位为省(市、自治区)、3-6位为县区、第7-10位数字是出生年、11-12位是出生月份、13-14是出生日期、第17位是性别,奇数为男性,偶数为女性,第18位是校验位。
如果身份证号码的其中一位填错了(包括最后一个校验位),则校验算法可以检测出来。如果身份证号的相邻2位填反了,则校验算法可以检测出来。校验规则如下:‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬
将前面的身份证号码17位数分别乘以不同的系数。从第一位到第十七位的系数分别为:7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2。
将这17位数字和系数相乘的结果相加。 用加出来和除以11,看余数只可能是:0-1-2-3-4-5-6-7-8-9-10
分别对应的最后一位身份证的号码为:1-0-X-9-8-7-6-5-4-3-2
通过上面得知如果余数是2,就会在身份证的第18位数字上出现罗马数字的X(大写英文字母X)。如果余数是10,身份证的最后一位号码就是2。
用户输入一个身份证号,校验其是否是合法的身份证号码。
如身份证号码不合法输出:‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬
身份证校验位错误!‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬
如身份证号码合法且地区码在输入中存在则分别在4行中分别输出:‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬
身份证号码校验为合法号码! 地区:x省(市、自治区)x 出生:x年x月x日
性别:‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬
如身份证号码合法但地区码在输入中不存在则输出:‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬
地区码不存在‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬
其中x代表从身份证中获取的省、县区,出生年、月、日和性别

输入

一个18位身份证号,末位为数字或大写字母X
地区信息
end

输出

如身份证号码不合法输出 '身份证校验位错误!'‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬
如身份证号码合法则分别在4行中分别输出:‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬
身份证号码校验为合法号码! 
地区:x省(市、自治区)x
出生:x年x月x日 
性别:x

样例输入 复制

220221197705166533
220000 吉林
220221 永吉县
end

样例输出 复制

身份证号码校验为合法号码!
地区:吉林省(市、自治区)永吉县
出生:19770516日
性别:男
id=input()
coefficient=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]#系数
last=['1','0','X','9','8','7','6','5','4','3','2']#余数对应末位编码
sum=0
dic={}
while(1): #直到读入end停止
    lst=input().split() #以空格为标志split编码和对应地区
    if(lst[0]=='end'):
        break
    if lst[0][2:6]=='0000': #精简省份的键值
        lst[0]=lst[0][0:2]
    dic[lst[0]]=lst[1] #将省份键值对存入字典
for i in range(17): #计算0~16*系数
    sum+=(ord(id[i])-(ord('0')))*coefficient[i]
sum%=11
if last[sum]!=id[-1]:
    print("身份证校验位错误!")
else:
    print("身份证号码校验为合法号码! ")
    flag=1 #默认可以在字典中找到对应地区
    if dic.get(id[0:2],0)!=0:
        province=dic.get(id[0:2],0)
    else:
        flag=0
        print("地区码不存在")
    if flag==1: #如果省码存在,查找县区
        if dic.get(id[0:6],0)!=0:
            county=dic.get(id[0:6],0)
        print("地区:%s省(市、自治区)%s"%(province,county))
    print("出生:%s年%s月%s日"%(id[6:10],id[10:12],id[12:14]))
    if eval(id[16])%2==1:
        print("性别:男")
    else:
        print("性别:女")

你可能感兴趣的:(buctoj,python)