浙大版《Python 程序设计》题目集 1~3章

  • Post author:
  • Post category:python



写在前面:

有编程基础的同学可以直接刷(不会语法再看文档,若没有那就边看书边刷

浙大版《Python 程序设计》题目集1~3章


题目链接:

浙大版《Python 程序设计》题目集


文章目录



第1章-1 从键盘输入两个数,求它们的和并输出 (30 分)

a = int(input())
b = int(input())

print(a+b)



第1章-2 从键盘输入三个数到a,b,c中,按公式值输出 (30 分)

a,b,c = map(int,input().split(' '))

print(b*b - 4*a*c)



第1章-3 输出“人生苦短,我学Python” (10 分)

print("人生苦短,我学Python")



第2章-1 计算 11+12+13+…+m (30 分)

m = int(input())

sum = 0
for i in range(11,m+1,1):
    sum += i
print("sum = %d"%sum)



第2章-2 计算分段函数[1] (10 分)

x = float(input())
if x != 0:
    print("f(%.1f) = %.1f"%(x,1/x))
else:
    print("f(%.1f) = %.1f"%(x,0))



第2章-3 阶梯电价 (15 分)

n = int(input())

if n < 0:
    print("Invalid Value!")
elif n <= 50:
    print("cost = %.2f"%float(n*0.53))
else :
    print("cost = %.2f"%float((n - 50)*(0.53+0.05) + 50 * 0.53))



第2章-4 特殊a串数列求和 (20 分)

a,n = map(int,input().split(' '))

d,t,s = 1,0,0

for i in range(n):
    s += a*d+t
    t += a*d
    d *= 10

print("s = %d"%s)



第2章-5 求奇数分之一序列前N项和 (15 分)

n = int(input())

fz = 1.0
fm = 1.0
s = 0.0

for i in range(n):
    s += fz/fm
    fm += 2

print("sum = %.6f"%s)



第2章-6 求交错序列前N项和 (15 分)

n = int(input())

fz = 1.0
fm = 1.0
flag = 1.0
s = 0.0

for i in range(n):
    s += fz/fm*flag
    flag *= -1
    fz += 1
    fm += 2

print("%.3f"%s)



第2章-7 产生每位数字相同的n位数 (30 分)

a,b = map(int,input().split(','))
print(str(a)*b)



第2章-8 转换函数使用 (30 分)

def cal(a,b):
    s = 0
    d = 1
    while a :
        s += a%10*d
        d *= b
        a//=10
    return s

a,b = map(int,input().split(','))
print(cal(a,b))



第2章-9 比较大小 (10 分)

list = list(map(int,input().split(' ')))
list.sort()

for i in range(0,3,1):
    if i:
        print('->',end = "")
    print(list[i],end = "")



第2章-10 输出华氏-摄氏温度转换表 (15 分)

def main():
    l, r = map(int, input().split(' '))
    if l > r:
        print("Invalid.")
        return
    print("fahr celsius")
    for i in range(l, r+1, 2):
        print("%d%6.1f" %(i,float(5 * (i - 32) / 9)))
if __name__ == '__main__':
    main()



第2章-11 求平方与倒数序列的部分和 (15 分)

def main():
    m,n = map(int,input().split(' '))
    s = 0
    for i in range(m,n+1,1):
        s += (i * i) + 1/i
    print("sum = %.6f"%s)
if __name__ == '__main__':
    main()



第2章-12 输出三角形面积和周长 (15 分)

在这里插入图片描述

import math
def main():
    lis = list(map(int,input().split(' ')))
    lis.sort()
    if lis[0] + lis[1] <= lis[2]:
        print("These sides do not correspond to a valid triangle")
        return
    a = lis[0]
    b = lis[1]
    c = lis[2]
    s = (a+b+c)/2
    area = math.sqrt(s * (s - a) * (s - b) * (s - c))
    print("area = %.2f; perimeter = %.2f"%(area,a+b+c))
if __name__ == '__main__':
    main()



第2章-13 分段计算居民水费 (10 分)

def main():
    x = int(input())
    y = 0
    if x <= 15:
        y = 4*x/3
    else:
        y = 2.5*x-17.5
    print("%.2f"%y)
if __name__ == '__main__':
    main()



第2章-14 求整数段和 (15 分)

def main():
    a,b = map(int,input().split(' '))
    cnt = 0
    s = 0
    for i in range(a,b+1,1):
        cnt+=1
        print("%5d"%i,end="")
        if(cnt % 5 == 0):print("")
        s+=i
    if(cnt % 5 != 0): print("")
    print("Sum = %d"%s)
if __name__ == '__main__':
    main()



第3章-1 大于身高的平均值 (10 分)

def main():
    list1 = list(map(int,input().split(' ')))
    list2 = []
    avg = 0
    for i in range(len(list1)):
        avg += list1[i]
    avg/=len(list1)
    for i in range(len(list1)):
        if list1[i]>avg:
            list2.append(list1[i])
    for i in range(len(list2)):
        print("%d "%list2[i],end="")
if __name__ == '__main__':
    main()



第3章-2 查验身份证 (15 分)

nums = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
strs = ['1','0','X','9','8','7','6','5','4','3','2','1']

def judge(s):
    sum = 0
    for i in range(len(s) - 1):
        if s[i].isdigit():
            sum += int(s[i]) * nums[i]
        else:
            return False
    if strs[sum%11] == s[len(s) - 1]:
        return True
    else:
        return False
def main():
    n = int(input())
    list1 = []
    for i in range(n):
        list1.append(input())
    flag = True
    for i in range(n):
        if judge(list1[i]) == False:
            flag = False
            print(list1[i])
    if flag:
        print("All passed")
if __name__ == '__main__':
    main()



第3章-3 输出字母在字符串中位置索引 (20 分)

def main():
    s = input()
    a,b = map(str,input().split(' '))
    for i in range(len(s) - 1,0,-1):
        if b == s[i]:
            print("%d %s"%(i,b))
        if a == s[i]:
            print("%d %s"%(i,a))
if __name__ == '__main__':
    main()



第3章-4 查找指定字符 (15 分)

def main():
    c = input()
    s = input()
    s = s[::-1]
    if s.find(c) != -1:
        print("index = %d"%(len(s) - s.find(c) - 1))
    else:
        print("Not Found")
if __name__ == '__main__':
    main()



第3章-5 字符转换 (15 分)

def main():
    s = input()
    res = ""
    for i in range(len(s)):
        if s[i].isdigit():
            res += s[i]
    print(int(res))
if __name__ == '__main__':
    main()



第3章-6 求整数序列中出现次数最多的数 (15 分)

def main():
    list1 = list(map(int,input().split(' ')))
    list1 = list1[1::]
    list2 = []
    for i in range(0,len(list1),1):
        cnt = list1.count(list1[i])
        list2.append(cnt)
    ans = max(list2)
    res = 0
    for i in range(len(list2)):
        if ans == list2[i]:
            res = list1[i]
            break
    print("%d %d"%(res,ans))
if __name__ == '__main__':
    main()



第3章-7 求最大值及其下标 (20 分)

def main():
    n = int(input())
    list1 = list(map(int,input().split(' ')))
    res = max(list1)
    idx = 0
    for i in range(n):
        if res == list1[i]:
            idx = i
            break
    print("%d %d"%(res,idx))
if __name__ == '__main__':
    main()



第3章-8 字符串逆序 (15 分)

def main():
    s = input()
    s = s[::-1]
    print(s)
if __name__ == '__main__':
    main()



第3章-9 字符串转换成十进制整数 (15 分)

def cal(s):
    s = s[::-1]
    d = 1
    sum = 0
    for i in range(len(s)):
        if(s[i].isdigit()):
            sum += d*int(s[i])
        else:
            sum += d*int(s[i],16)
        d*=16
    return sum
def main():
    s = input()
    flag = False
    res = ""
    s = s.lower()
    for i in range(len(s)):
        if(s[i] == '-' and len(res) == 0):
            flag = True
        if(s[i].isdigit() or ("a"<=s[i]<="f")):
            res += s[i]
    ans = cal(res)
    if flag:
        ans = -ans
    print(int(ans))
if __name__ == '__main__':
    main()



第3章-10 统计大写辅音字母 (15 分)

def judge(s):
    if s == 'A' or s == 'E' or s == 'I' or s == 'O' or s == 'U':
        return False
    else:
        return True
def main():
    s = input()
    res = 0
    for i in range(len(s)):
        if(s[i].isupper()):
            if(judge(s[i])):
                res += 1
    print(int(res))
if __name__ == '__main__':
    main()



第3章-11 字符串排序 (20 分)

def main():
    list1 = list(map(str,input().split(' ')))
    list1.sort()
    print("After sorted:")
    for i in range(len(list1)):
        print(list1[i])
if __name__ == '__main__':
    main()



第3章-12 求整数的位数及各位数字之和 (15 分)

def main():
    s = input()
    cnt = len(s)
    res = 0
    for i in range(cnt):
        res += int(s[i])
    print("%d %d"%(cnt,res))
if __name__ == '__main__':
    main()



第3章-13 字符串替换 (15 分)

def change(s):
    return chr(26 - (ord(s) - ord('A')) + ord('A') - 1)
def main():
    s = list(input())
    lens = len(s)
    res = ''
    for i in range(lens):
        if(str(s[i]).isupper()):
            s[i] = change(s[i])
        res += s[i]
    print(res)
if __name__ == '__main__':
    main()



第3章-14 字符串字母大小写转换 (15 分)

def main():
    s = input()
    s = s[0:len(s)-1]
    print(s.swapcase())
if __name__ == '__main__':
    main()



第3章-15 统计一行文本的单词个数 (15 分)

def main():
    s = input()
    list1 = s.split()
    print(len(list1))
if __name__ == '__main__':
    main()



第3章-16 删除重复字符 (20 分)

def main():
    s = input()
    list1 = list(set(s))
    list1.sort()
    print(''.join(list1))
if __name__ == '__main__':
    main()



第3章-17 删除字符 (30 分)

def main():
    s = input().strip()
    c = input().strip()
    s = s.replace(c.upper(), "")
    s = s.replace(c.lower(), "")
    print("result: %s"%s)
if __name__ == '__main__':
    main()



第3章-18 输出10个不重复的英文字母 (30 分)

def main():
    s = input()
    set1 = set([])
    res = ""
    for i in range(len(s)):
        if len(set1) == 10:
            break
        if s[i].isalpha() and s[i].lower() not in set1:
            set1.add(s[i].lower())
            res += s[i]
    if len(set1) < 10:
        print("not found")
    else:
        print(res)
if __name__ == '__main__':
    main()



第3章-19 找最长的字符串 (15 分)

def main():
    n = int(input())
    list1 = []
    list2 = []
    for i in range(n):
        list1.append(input())
        list2.append(len(list1[i]))
    lens = max(list2)
    res = list1[list2.index(lens)]
    print("The longest is: %s"%res)
if __name__ == '__main__':
    main()



第3章-20 逆序的三位数 (10 分)

def main():
    s = input()
    s = s[::-1]
    list1 = list(s)
    i = 0
    while list1[i] == 0:
        i += 1
    if i == len(list1):
        print(0)
    else:
        print(int(s[i:len(s)]))

if __name__ == '__main__':
    main()



第3章-21 判断回文字符串 (15 分)

def main():
    s1 = input()
    s2 = list(s1)
    s2.reverse()
    s2 = ''.join(s2)
    print(s1)
    if s1 == s2:
        print("Yes")
    else:
        print("No")
if __name__ == '__main__':
    main()



第3章-22 输出大写英文字母 (15 分)

def main():
    s = input()
    hashs = set([])
    res = ""
    for i in range(len(s)):
        if s[i].isupper() and s[i] not in hashs:
            hashs.add(s[i])
            res += s[i]
    if len(res) == 0:
        res = "Not Found"
    print(res)
if __name__ == '__main__':
    main()



版权声明:本文为weixin_45821690原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。