Python语言程序设计(嵩天)——考试编程题答案

  • 求输入正整数n的各阶乘之和
n=eval(input())
if int!=type(n):
  print("输入有误,请输入正整数")
elif n==0:
  print("输入有误,请输入正整数")
else:
  t=1
  s=0
  for i in range(1,n+1):
    for j in range(1,i+1):
      t=t*j
    s=s+t
    t=1
  print(s)
  • 根据考试分数计算成绩等级
try:
    score = eval(input())
    if score>100 or score<0:
        print("输入有误!")
    else:
        a = int(100 / 10)
        if a == 10 or a == 9:
            print("输入成绩属于A级别。")
            print("祝贺你通过考试!")
        elif a == 8:
            print("输入成绩属于B级别。")
            print("祝贺你通过考试!")
        elif a == 7:
            print("输入成绩属于C级别。")
            print("祝贺你通过考试!")
        elif a == 6:
            print("输入成绩属于D级别。")
            print("祝贺你通过考试!")
        else:
            print("输入成绩属于E级别。")
except:
    print("输入有误!")
finally:
    print("好好学习,天天向上!")
  • 判断一个数是否是快乐数
def numSum(n):
    sum=0
    x=0
    while n!=0:
        x=n%10
        n=int(n/10)#此处比较重要,python默认除法返回小数,所以装换成int
        sum=sum+x*x
    return sum
num=eval(input())
if num==1:
  print(True)
else:
  count = num
  limit=0
  while count!=1 and limit<1000:
    count=numSum(count)
    if count==4:
      print(False)
      break
    limit+=1
  if count==1:
    print(True)
  • 三次登录问题
flag=False
for i in range(3):
    name=input()
    powd=input()
    if name=="Kate" and powd=="666666":
        print("登录成功!")
        flag=True
        break
if flag==False:
    print("3次用户名或者密码均有误!退出程序。")
#本人觉得此题有问题,判断程序和题目描述不符,不过这样写也可以得满分
  • 根据正整数n,输出合适的括号组合
class Solution:
    def generateParenthesis(self, n):
        res = []
        self.helper(res, 1,0, n, "(")
        return res

    def helper(self, res, i, j, n, path):
        if i > n:
            return
        if i == n and j == n:
            res.append(path)
            return

        elif i > j:
            self.helper(res, i + 1, j, n, path + "(")
            self.helper(res, i, j + 1, n, path + ")")
        elif i == j:
            self.helper(res, i + 1, j, n, path + "(")
num=eval(input())
object=Solution()
print(object.generateParenthesis(num))
  • 菲式数列问题
def fib(n):
    if n<=2:
        return 1
    return fib(n-1)+fib(n-2)
num=eval(input())
list=[]
for i in range(num+2):#此处循环次数多一些无所谓
    if i==0:
        print(0,end=', ')
    elif fib(i)<=num:#只有比num小的数才加到list中
        list.append(fib(i))
        print(fib(i),end=', ')
sum=0
for j in range(len(list)):
    sum=sum+list[j]
avg=int(sum/len(list))
print(sum,end=', ')
print(avg)
  • 哈姆雷特词频统计问题
def getText():
    txt=open("hamlet.txt","r").read()
    txt=txt.lower()
    for ch in '!"#$%&()*+,-./:;<=>?@[\\]·^_{|}~':
        txt=txt.replace(ch," ")
    return txt
hamletTxt=getText()
words=hamletTxt.split()
counts={}
for word in words:
    counts[word]=counts.get(word,0)+1
items=list(counts.items())
items.sort(key=lambda x:x[1],reverse=True)
for i in range(10):
    word,count=items[i]
    print("{0:<10},{1:>5}".format(word,count))
  • 水仙花问题
def jude(n):
    m=n
    a=int(m/100)
    m=m%100
    b=int(m/10)
    m=m%10
    if pow(m,3)+pow(a,3)+pow(b,3)==n:
        return True
    else:
        return False
for i in range(100,1000):
    if jude(i)==True and i!=407:
        print(i,end=',')
    elif i==407:
        print(i)
  • 竖着输出字符串
str1=input()
for i in range(len(str1)):
  print(str1[i])
  • 凯撒密码

str=input()
strout=''
for i in range(len(str)):
    if 'a' <= str[i] and str[i] <= 'z':
        strout+=chr((ord(str[i])-97+3)%26+97)
    elif 'A' <= str[i] and str[i] <= 'Z':
        strout += chr((ord(str[i]) - 65 + 3) % 26 + 65)
    else:
        strout += str[i]
print(strout)

Python语言程序设计(嵩天)——考试编程题答案_第1张图片

你可能感兴趣的:(python学习笔记)