备战蓝桥杯大赛:Python必刷题单之基础练习

备战蓝桥杯大赛:Python必刷题单之基础练习,第1张

备战蓝桥杯大赛:Python必刷题单之基础练习 1. 入门训练 A + B 问题

题目链接

a, b = map(int, input().split())
print(a + b)
圆的面积

题目链接

import math
r = int(input())
area = math.pi * r * r
print("%.7f" % area)

数学函数相关内容

序列求和

题目链接

# 等差数列求和,注意数据范围和整除
n = int(input())
ans = (1 + n) * n // 2
print(ans)
闰年判断

题目链接

y = int(input())
if y % 400 == 0 or y % 4 == 0 and y % 100 != 0:
  print("yes")
else:
  print("no")
阶乘计算

题目链接

n = int(input())
f = 1
for i in range(1, n + 1):
  f *= i
print(f)
Fibonacci数列

题目链接

n = int(input())
# 列表初始化,多申请10个空间防止越界
a = [1] * (n + 10)
for i in range(3, n + 1):
  a[i] = (a[i - 1] + a[i - 2]) % 10007
print(a[n])
2. 基础练习 矩形相交面积

题目链接

a = list(map(float, input().split()))
b = list(map(float, input().split()))

# 相交矩形左右两边的x坐标
x1 = max(min(a[0], a[2]), min(b[0], b[2]))
x2 = min(max(a[0], a[2]), max(b[0], b[2]))
# 相交矩形上下两边的y坐标
y1 = max(min(a[1], a[3]), min(b[1], b[3]))
y2 = min(max(a[1], a[3]), max(b[1], b[3]))

if x1 >= x2 or y1 >= y2:
    print("0.00")
else:
    area = (x2 - x1) * (y2 - y1)
    print("%.2f" % area)
时间转换

题目链接

n = int(input())
h = n // 3600
m = n % 3600 // 60
s = n % 3600 % 60
print("%d:%d:%d" % (h, m, s))
特殊的数字

题目链接

for n in range(100, 1000):
  o = n % 10
  t = n // 10 % 10
  h = n // 100
  if o ** 3 + t ** 3 + h ** 3 == n:
    print(n)
查找整数

题目链接

n = int(input())
a = list(map(int, input().split()))
x = int(input())
for i in range(n):
    if a[i] == x:
        print(i + 1)
        break;
else:
    print(-1)

Python列表相关内容

数列特征

题目链接

n = int(input())
a = list(map(int, input().split()))
print(max(a))
print(min(a))
print(sum(a))
数列排序

题目链接

n = int(input())
a = list(map(int, input().split()))
a.sort()
for x in a:
  print(x, end = " ")
Huffuman树

题目链接

n = int(input())
a = list(map(int, input().split()))
ans = 0
while len(a) > 1:
    a.sort()
    p = a[0] + a[1]    
    a.pop(0)
    a.pop(0)
    a.append(p)
    ans += p   
print(ans)
龟兔赛跑预测

题目链接

v1,v2,t,s,l = map(int, input().split())
s1 = 0
s2 = 0
t1 = 0
t2 = l // v2
while s1 < l:
    if s1 - s2 >= t:
        s2 += v2 * s
        t1 += s
    else:
        s1 += v1
        s2 += v2
        t1 += 1
if t1 > t2:
    print("T")
elif t1 < t2:
    print("R")
else:
    print("D")
print(min(t1, t2))

十进制转十六进制

题目链接

n = int(input())
s = format(n, "X")
print(s)
  • format函数相关内容
  • 字符串format函数相关内容
十六进制转十进制

题目链接

s = input()
d = int(s, 16)
print(d)

Python进制转换相关内容

十六进制转八进制

题目链接

n = int(input())
for i in range(n):
    s = input()
    # 将十六进制转十进制
    d = int(s, 16)
    # 将十进制转八进制
    s = format(d, "o")
    print(s)
01字串

题目链接

for i in range(32):
    s = format(i,"0>5b")
    print(s)
字符串对比

题目链接

a = input()
b = input()
if len(a) != len(b):
    print(1)
elif a == b:
    print(2)
elif a.lower() == b.lower():
    print(3)
else:
    print(4)
FJ的字符串

题目链接

n = int(input())
s = ""
for i in range(n):
   # 拼接新字符串 = s + 新字母 + s
   s = s + chr(ord("A") + i) + s
print(s)

Python ASCII码与字符相互转换

字母图形

题目链接

n, m = map(int, input().split())
a = [chr(i + ord("A")) for i in range(m)]
c = ord('A')
for i in range(n):    
    print("".join(a))
    # 删除末尾字符
    a.pop()
    # 在头部插入新字符
    c = c + 1    
    a.insert(0, chr(c))
回文数

题目链接

for i in range(1000, 10000):
    s = str(i)
    # 通过切片将字符串反转
    if(s == s[::-1]):
        print(s)

字符串切片相关内容

特殊回文数

题目链接

n = int(input())
for i in range(10000, 1000000):
    s = str(i)
    # 通过切片将字符串反转
    if(s == s[::-1]):
        a = [int(c) for c in s]
        if sum(a) == n:
            print(s)
判断质数

题目链接

n = int(input())
i = 2
while i * i <= n:
  if n % i == 0:
    print("No")
    break
  i += 1
else:
  print("Yes")
分解质因数

题目链接

# 质数判断
def is_prime(n):
    i = 2
    while i * i <= n:
        if n % i == 0:
            return 0
        i += 1
    else:
        return 1

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

for n in range(a, b + 1):
    if is_prime(n):
        print("%d=%d" % (n, n))
        continue
    //质因数分解
    p = 2
    print("%d=" % n, end = "")
    while n > 1:
        while n % p == 0:
            print(p, end = "")
            n //= p
            if n > 1:
                print("*", end = "")
            else:
                print("")
        p = p + 1
杨辉三角形

题目链接

n = int(input())
f = []
# 初始化n行杨辉三角
for i in range(n):
    f.append([0] * (i + 1))
print(f)
# 计算杨辉三角
i = 0
while i < n:
    j = 0
    while j <= i:        
        if(j == 0 or j == i):
            f[i][j] = 1
        else:
            f[i][j] = f[i - 1][j] + f[i - 1][j - 1]
        j += 1
    i += 1
# 输出杨辉三角
i = 0
while i < n:
    j = 0
    while j <= i:
        print(f[i][j], end = " ")
        j += 1
    print("")
    i += 1

回形取数

题目链接

a = []
b = []
n, m = map(int, input().split())
# 读入序列
for i in range(n):
    a.append(list(map(int, input().split())))

# 从r行、c列开始取数
r = 0
c = 0
b.append(a[r][c])
a[r][c] = -1

# 已取个数
cnt = 1
while cnt < n * m:
    while r + 1 < n and a[r + 1][c] != -1:
        r = r + 1
        b.append(a[r][c])
        a[r][c] = -1
        cnt += 1
    while c + 1 < m and a[r][c + 1] != -1:
        c = c + 1
        b.append(a[r][c])
        a[r][c] = -1
        cnt += 1
    while r - 1 >= 0 and a[r - 1][c] != -1:
        r = r - 1
        b.append(a[r][c])
        a[r][c] = -1
        cnt += 1
    while c - 1 >= 0 and a[r][c - 1] != -1:
        c = c - 1
        b.append(a[r][c])
        a[r][c] = -1
        cnt += 1
# 将b列表中的数字用空格连接起来,避免末尾有多余空格
print(" ".join([str(i) for i in b]))
报时助手

题目链接

a = {0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five',
     6:'six', 7: 'seven', 8: 'eight', 9: 'nine', 10: 'ten', 11:'eleven',
     12: 'twelve', 13: 'thirteen', 14: 'fourteen', 15:'fifteen', 16: 'sixteen',
     17: 'seventeen', 18: 'eighteen', 19:'nineteen', 20: 'twenty', 21: 'twenty one',
     22: 'twenty two', 23: 'twenty three', 30: 'thirty', 40: 'forty', 50: 'fifty'}
       
h, m = map(int, input().split())

if m == 0:
    print("%s o'clock" % a[h])
else:
    print(a[h], end = " ")
    if m <= 20 or m == 30 or m == 40 or m == 50:
        print(a[m])
    elif m < 30:
        print(a[20] + " " + a[m - 20])
    elif m < 40:
        print(a[30] + " " + a[m - 30])
    elif m < 50:
        print(a[40] + " " + a[m - 40])
    else:
        print(a[50] + " " + a[m - 50])
数的读法

题目链接

a = { 0 : "ling", 1 : "yi", 2 : "er", 3 : "san", 4 : "si", 5 : "wu",
     6 : "liu", 7 : "qi", 8 : "ba", 9 : "jiu", 10 : "yi shi" }

def work(n, allowZero):
    # 千位
    q = n // 1000
    hasQ = 0
    if q > 0:
        # 千位不为0        
        print("%s qian" % a[q], end = " ")
        hasQ = 1
        allowZero = 1

    # 百位
    b = n // 100 % 10
    hasB = 0
    if b > 0:
        # 百位不为0
        if allowZero and not hasQ:
            print("ling %s bai" % a[b], end = " ")
        else:
            print("%s bai" % a[b], end = " ")
        hasB = 1 
        allowZero = 1

    # 十位
    s = n // 10 % 10
    hasS = 0          
    if s > 0:
        if allowZero and not hasB:
            print("ling %s shi" % a[s], end = " ")
        else:
            # 特殊处理:当十位为1时
            if s == 1:
                print("shi", end = " ")
            else:
                print("%s shi" % a[s], end = " ")
        hasS = 1
        allowZero = 1
        
    # 个位
    g = n % 10 
    hasG = 0  
    if g > 0:
        if allowZero and not hasS:
            print("ling %s" % a[g], end = " ")
        else:
            print("%s" % a[g], end = " ")
        hasG = 1
    
    return (hasQ or hasB or hasS or hasG)

n = int(input())
# 处理超过亿位的数字
t = n // 100000000
hasY = work(t, 0)
if hasY:
    print("yi", end = " ")
# 处理超过万位的数字
t = n // 10000 % 10000
hasW = work(t, hasY)
if hasW:
    print("wan", end = " ")
# 处理最低4位数字
t = n % 10000
work(t, hasW)
持续更新中…

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5495685.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存