测验六python编程题,Python程序设计 测验易错题总结

1.温度转换

t=input()if t[-1]=="J":

t=int(t[:-1])

t1=t/4.186

print("%.3fcal"%t1)else:

t=int(t[0:-3])

t2=t*4.186

print("%.3fcal"%t2)

#这一题不难,但要注意input()里面不要加提示语句,会报错的。

2.快乐的数字

n=input()

h=0

i=0while i<100:for x inn:

h+=int(x)**2

print(n)if h==1:print("True")break

else:

n=str(h)

h=0

i+=1

else:print("False")

3.斐波那契数列计算 B

描述:

斐波那契数列如下:

F(0) = 0, F(1) = 1

F(n) = F(n-1) + F(n-2)

编写一个计算斐波那契数列的函数,采用递归方式,输出不超过n的所有斐波那契数列元素

调用上述函数,完成如下功能:

用户输入一个整数n,输出所有不超过n的斐波那契数列元素、输出数列的元素和及平均数,输出按照顺序,用英文逗号和空格分割

此题目为自动评阅,请严格按照要求规范输入和输出。

输入

示例1:5

输出

示例1:

0, 1, 1, 2, 3, 5, 12, 2

n=eval(input()) #输出一个斐波那契数列元素

deffun(n):if n==0:return0elif n==1:return 1

else:return fun(n-1)+fun(n-2)

i=0

l=[]while i<=n: #输出从0到n的斐波那契数列元素

a=fun(i)

l.append(a)print("{},".format(a),end="") #0以后的元素前面都有空格

i+=1sums=sum(l)

average=sums//len(l)print("{},{}".format(sums,average)) #总和和平均数前面也有空格

4.站队顺序输出

描述:

有一群人站队,每人通过一对整数(h, k)来描述,其中h表示人的高度,k表示在此人前面队列中身高不小于此人的总人数。

实现一个算法输出这个队列的正确顺序。

输入格式:

输入格式为二维列表,即 list[list[]]形式

外层list包含队列中全部的人,内层list为[h,k]格式,代表个人信息。

输出格式:

输出格式为:list[list[int]]形式

与输入格式一样,需要按照队列顺序排列。

输入输出示例 :

示例 1

输入

[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]

输出

[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]

from operator importitemgetter

queue=eval(input())

queue.sort(key= itemgetter(1))

queue.sort(key= itemgetter(0), reverse =True)

output=[]for item inqueue:

output.insert(item[1], item)print(output)

5.合法括号组合的生成

描述:

给定括号的个数n,编写程序生成所有格式正确的括号组合。

输入格式 :输入一个整数。

输出格式:输出为一个列表,每个元素是一个字符串,表示一个可能的括号组合。

示例 :

输入:3

输出:['((()))', '(()())', '(())()', '()(())', '()()()']

defbrackets(List,s,left,right):if left==0 and right==0:

List.append(s)if left>0: #注意这里是if,不是elif

brackets(List,s+'(',left-1,right+1)if right>0: #注意这里是if,不是elif

brackets(List,s+')',left,right-1)returnList

left=eval(input())

right=0

List=[]

s=''brackets(List,s,left,right)print(List)

6.凯撒密码 B

pwd=input()

s=""

for x inpwd:

count=0if x in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":

c=(ord(x)+3-64)%26+64s+=chr(c)

count=1

if x in "abcdefghijklmnopqrstuvwxyz":

c=(ord(x)+3-97)%26+97s+=chr(c)

count=1

if count==0:

s+=xprint(s)

7. 3位水仙花数计算 B

for n in range(100,999):

a=str(n)

s=int(a[0])**3+int(a[1])**3+int(a[2])**3

if n==407:print(n)if s==n and n!=407:print(n,end=",")

8.词频统计之《哈姆雷特》

defgetTxt():

txt=open("hamlet.txt","r").read()

txt=txt.lower()for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_{|}`~''':

txt=txt.replace(ch," ")returntxt

txt=getTxt()

words=txt.split()

count={}for word inwords:

count[word]=count.get(word,0)+1items=list(count.items()) #二维列表

items.sort(key=lambda x:x[1],reverse=True)for i in range(10):

word,count=items[i]print("{:<10},{:>5}".format(word,count))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值