浙大版《Python 程序設計》題目集

代碼全是用的python 3.x語法

第1章-1 從鍵盤輸入兩個數,求它們的和並輸出 (30分)

x = int(input())
y = int(input())
print(x+y)

第1章-2 從鍵盤輸入三個數到a,b,c中,按公式值輸出 (30分)

x = input().split()
print(int(x[1])**2 - 4*int(x[0])*int(x[2]))

第1章-3 輸出“Python語言簡單易學” (10分)

s = "Python語言簡單易學"
print(s.encode("utf-8"))

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

x = int(input())
y = 0
for i in range(11, x+1):
    y += i
print("sum =",y)

第2章-2 計算分段函數[1] (10分)

a = float(input())
print("f({:.1f}) = {:.1f}".format(a, 1/a) if a != 0 else "f({:.1f}) = {:.1f}".format(a, a))

第2章-3 階梯電價 (15分)

a = int(input())
ans = 0
if a <= 50:
    ans = a*0.53
else:
    ans = 50*0.53 + (a -50)*0.58
print("cost = {:.2f}".format(ans) if a > 0 else "Invalid Value!")

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


a, b = input().split()
ans = 0
for i in range(1, int(b)+1):
    ans += int(a*i)
    # print(a*i)
print("s = %s"%ans)

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

n = int(input())
s = 0.0
index = 1
for i in range(0, n):
    s += 1.0/index
    index += 2
print("sum = {:.6f}".format(s))

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

n = int(input())
index = 1
s = 0.0
for i in range(1, n+1):
    if i % 2 == 0:
        s -= i/index
    else:
        s += i/index
    index += 2
print("{:.3f}".format(s))

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

a, b = input().split(",")
print(a.strip()*int(b))

第2章-8 轉換函數使用 (30分)

a, b = input().split(",")
print(int(a, int(b)))

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

lst = list(map(lambda x: int(x), input().split()))
lst.sort()
print("{}->{}->{}".format(lst[0], lst[1], lst[2]))

第2章-10 輸出華氏-攝氏溫度轉換表 (15分)

a, b = input().split()
if int(a) > int(b):
    print("Invalid.")
else:
    print("fahr celsius")
    for i in range(int(a), int(b)+1, 2):
        print("{}{:6.1f}".format(i, 5*(i-32)/9))

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

a, b = input().split()
s = 0.0
for i in range(int(a), int(b)+1):
    s += i*i + 1/i
print("sum = {:.6f}".format(s))

第2章-12 輸出三角形面積和周長 (15分)

from math import sqrt
a, b, c = map(lambda x: float(x), input().split())
if a + b <= c or a + c <= b or b + c <= a:
    print("These sides do not correspond to a valid triangle")
else:
    p = (a+b+c)/2
    area = sqrt(p*(p-a)*(p-b)*(p-c))
    perimeter = a+b+c
    print("area = {:.2f}; perimeter = {:.2f}".format(area, perimeter))

第2章-13 分段計算居民水費 (10分)

a = float(input())
ans = 0.0
if a <= 15:
    ans = a*4/3
else:
    ans = 2.5*a - 17.5
print("{:.2f}".format(ans))

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

a, b = map(lambda x: int(x), input().split())
index = 0
s = 0
for i in range(a, b+1):
    print("{:5d}".format(i), end="")
    index += 1
    s += i
    if index % 5 == 0:
        print()
if index % 5 != 0:
    print()
print("Sum = %s"%s)

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

lst = list(map(lambda x: int(x), input().split()))
avg = sum(lst)/len(lst)
for x in lst:
    if x > avg:
        print(x, end=" ")

第3章-2 查驗身份證 (15分)

n = int(input())
dic = {0: '1', 1: '0', 2: 'X', 3: '9', 4: '8', 5: '7', 6: '6', 7: '5', 8: '4', 9: '3', 10: '2'}
weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
lst = []
for i in range(n):
    b = input()
    s = 0
    u = 0
    for x in b[:-1]:
        if not x.isdigit():
            u = 1
    if u == 1:
        lst.append(b)
        continue
    for x in b[:-1]:
        s += (ord(x)-ord('0'))*weight[u]
        u += 1
    if ord(dic[s % 11]) != ord(b[-1]):
        lst.append(b)

if len(lst) == 0:
    print("All passed")
else:
    for x in lst:
        print(x)

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

s = input()
a, b = input().split()
for i in range(len(s)-1, 0, -1):
    if ord(s[i]) == ord(b[0]):
        print(i, b)
for i in range(len(s)-1, 0, -1):
    if ord(s[i]) == ord(a[0]):
        print(i, a)

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

c = input()
s = input()
index = -1
for i in range(len(s)):
    if ord(s[i]) == ord(c[0]):
        index = i
print("index = %s" % index if index != -1 else "Not Found")

第3章-5 字符轉換 (15分)

a = input()
print(int("".join([x for x in a if x.isdigit()])))

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

from collections import Counter
lst = list(map(lambda x: int(x), input().split()))
ans = Counter(lst[1:]).most_common(1)[0]
print(ans[0], ans[1])

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

n = int(input())
lst = list(map(lambda x: int(x), input().split()))
index = 0
for x in range(n):
    if lst[x] > lst[index]:
        index = x
print("%d %d" % (lst[index], index))

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

a = input()
print(a[::-1])

第3章-9 字符串轉換成十進制整數 (15分)

a = input().split("#")[0].lower()
b = ['a','b','c','d','e','f']
countN = 0
for x in a:
    if ord(x) == ord('-'):
        countN += 1
    if x.isdigit() or x in b:
        break
lst = [x for x in a if x.isdigit() or x in b]
if len(lst)==0:
    print(0)
else:
    print(int("".join(lst), 16) if countN == 0 else -int("".join(lst), 16))

第3章-10 統計大寫輔音字母 (15分)

res = sum([1 if ch.isalpha() and ch.isupper() and ch not in set(["A", "O", 'E', 'I', 'U']) else 0 for ch in input()])
print(res)

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

a = input().split()
a.sort()
print("After sorted:")
for key in a:
    print(key)

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

x = input()
a = len(x)
b = 0
for i in x:
    b = b + int(i)
print(a, b)

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

x = input()
for ch in x:
    if ch.isupper():
        ch = chr(ord('Z') - ord(ch) + ord('A'))
    print(ch, end = "")

第3章-14 字符串字母大小寫轉換 (15分)

x = input()
for ch in x:
    if ch.isupper():
       ch = ch.lower()
    elif ch.islower():
        ch = ch.upper()
    elif ord(ch)==ord('#'):
        break
    print(ch, end = "")

第3章-15 統計一行文本的單詞個數 (15分)

print(len(input().split()))

第3章-16 刪除重複字符 (20分)

x = set(input())
y = list(x)
y.sort()
print("".join(y))

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

a = input().strip()
b = input().strip()
print("result: ", end="")
for x in a:
    if ord(b[0]) == ord(x) or ord(x.lower()) == ord(b[0]) or ord(x.upper()) == ord(b[0]):
        continue
    else:
        print(x, end="")

第3章-18 輸出10個不重複的英文字母 (30分)

a = input()
newa = []
for x in a:
    if x not in newa and x.upper() not in newa and x.lower() not in newa:
        newa.append(x)
lst =[x for x in newa if x.isalpha()]
print("".join(lst[:10]) if len(lst) >= 10 else "not found")

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

n = int(input())
ans = ""
for i in range(n):
    b = input()
    if len(b) > len(ans):
        ans = b

print("The longest is: %s"%ans)

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

a = input()
b = a[::-1]
i = 0
while ord(b[i]) == ord('0'):
    i += 1
print(b[i:])

第3章-21 判斷迴文字符串 (15分)

a = input()
b = a[::-1]
print(a)
print("Yes" if a == b else "No")

第3章-22 輸出大寫英文字母 (15分)

lst = input()
ans = ""
for x in lst:
    if x.isupper() and x not in ans:
        ans += x
print(ans if len(ans) > 0 else "Not Found")
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章