2蓝桥杯练习系统 python

记录蓝桥杯刷题过程

上接1:https://blog.csdn.net/m0_45813696/article/details/115583882


# 目录
# 一、基础练习
## BASIC-20数的读法(判断 函数)
d = {'1':'yi', '2':'er','3':'san','4':'si','5':'wu','6':'liu','7':'qi','8':'ba','9':'jiu','0':'ling'}
n = input().strip()       #str.strip()就是把这个字符串头和尾的空格,以及位于头尾的\n \t之类给删掉
a = []
b = []                          #b存的是逆序的读法,最后再逆序输出即为结果

def numexchange(i):           #每四位一组代入
    i = i[::-1]                         #逆序
    for j in range(len(i)):
        if j == 0:
            if i[j]!='0':
                b.append(d[i[j]])
        elif j == 1:
            if i[j]=='0'  and  i[0]!='0':
                b.append(d['0'])
            elif i[j] == '1' and len(i)==2:
                b.append('shi')
            elif i[j]!='0':
                b.append('shi')
                b.append(d[i[j]])
        elif j == 2:
            if i[j]=='0'  and i[1]!='0':
                b.append(d['0'])
            elif i[j]!='0':
                b.append('bai')
                b.append(d[i[j]])
        elif j == 3:
            if  i[j]!='0':
                b.append('qian')
                b.append(d[i[j]])
            elif i[j]=='0'  and i[2]!='0':
                b.append(d['0'])

while n!='':  #倒着读,每四位为一组字符串
    if len(n)>=4:
        s = n[-4:] #取后四位
        n = n[:-4] #删除后四位
        a.append(s) 
    else:
        a.append(n)
        n = ''
for i in range(len(a)):
    if i == 0:
        numexchange(a[i])
    elif i == 1:
        b.append('wan')
        numexchange(a[i])
    elif i == 2:
        b.append('yi')
        numexchange(a[i])
        
for i in range(len(b)-1,-1,-1):         #因为range(a,b) 是左开右闭,如果(len(b)-1,0,-1),则b[0]不会被包含到
    if i == len(b)-1:
        print(b[i],end='')
    else:
        print(' %s'%(b[i]),end='')   #不是第一位数字,先输空格,再输读音

BASIC-21 Sine之舞1(字符串 递归 递推)

def An(n,i=1):
    if i==n:
        return 'sin('+str(n)+')'
    else:
        if i%2==0:
            s='+'
        else:
            s='-'
        return 'sin('+str(i)+s+An(n,i+1)+')'

def Sn(m,i=1):
    if m==1:
        return An(m)+'+'+str(i)
    else:
        return '('+Sn(m-1,i+1)+')'+An(m)+'+'+str(i)

num=int(input())
print(Sn(num))

BASIC-21 Sine之舞2(字符串 递归 递推)

N=int(input())
def An(n):
    for i in range(1,n):
        print('sin('+str(i),end='')
        if i%2==0:
            print('+',end='')
        else:
            print('-',end='')
    print('sin('+str(n),end='')
    for j in range(0,n):
        print(')',end='')
    return ''               #如果不加return ''  ,会默认返回None
def Sn(n):
    for i in range(1,n):
        print('(',end='')
    for j in range(1,n+1):
        print(str(An(j))+'+'+str(n+1-j),end='')
        if j!=n:
            print(')',end='')
    return ''
a=Sn(N)
print(a)

BASIC-22 FJ的字符串(字符串 递归)

n=int(input())
s=str()
def An(i):
    if i>1:
        return An(i-1)+(chr(ord('A')+i-1))+An(i-1)
    elif i==1:
        return 'A'

s=An(n)
for i in range(len(s)):
    print(s[i],end='')

BASIC-23 芯片测试(算法基础 统计 二维数组)

n=int(input())
test=[]

for i in range(n):
    test.append(list(map(int,input().split())))  #test列表存放芯片的测试结果

for j in range(n):          #利用“好芯片比坏芯片多”这一条件
    num=0
    for i in range(n):
        if test[i][j]==1:
            num+=1
    if num>n/2:
        print(j+1,end=' ')

BASIC-24 龟兔赛跑预测(数组 模拟)

v1,v2,t,s,x=map(int,input().split())    #v1,v2分别是兔子和乌龟的速度,t是兔子的领先阀值,s是兔子领先t米后偷懒的时间,x跑道长度
r1=0;t1=0;time=0               #分别是Rabbit和Turtle的跑步里程,裁判手里的计时器
while r1<x and t1<x:
    time+=1      #时间按秒增加
    r1+=v1            #v1(表示每秒兔子能跑v1  米)
    t1+=v2
    if (r1-t1)>=t and t1<x and r1<x:
        for j in range(s):
            t1+=v2
            time+=1
            if r1>=x or t1>=x:
                break
if r1==t1:
    print('D')
elif r1>t1:
    print('R')
else:
    print('T')
print(time)

BASIC-25 回形取数(二维数组 循环)

import math  
m,n=map(int,input().split())               #行数和列数
list1=[]                  #输入
ans=[]                   #输出
for i in range(0,m):
    list1.append(input().split())

for j in range(0,math.ceil(min(m,n)/2)):   #math.ceil()函数是用来向上取整,向下取整直接整数相除即可,math.ceil(min(r,c)/2表示要转的圈数
    for x in range(j,m-j):                     #将第j圈的左“1”字形x放入ans;
        ans.append(list1[x][j])
    for y in range(j+1,n-j):                 #将第j圈的下“一”字形x放入ans;
        ans.append(list1[m-1-j][y])
    if n-1>2*j:                                    #判断一下是否还有多余的列需要转圈
        for p in range(m-j-2,j-1,-1):      #将第j圈的右“1”字形放入ans;
            ans.append(list1[p][n-1-j])
    if m-1>2*j:
        for q in range(n-j-2,j,-1):          #将第j圈的上“一”字形放入ans;
            ans.append(list1[j][q])
for x in ans:
    print(x,'',end='')

BASIC-26 报时助手(字符串 条件判断)

d={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',30:'thirty',40:'forty',50:'fifty'}

nowtime=list(map(int,input().split()))   #输入h,m添加到列表nowtime
for i in range(len(nowtime)):
    if nowtime[1]==0:                             #当输入的m==0时
        print(d[nowtime[i]]+" o'clock")
        break
    elif nowtime[i]<=20 or nowtime[i]%10==0:
        print(d[nowtime[i]],end=' ')
    else:
        print(d[nowtime[i]-nowtime[i]%10]+' '+d[nowtime[i]%10],end=' ')

BASIC-27 2n皇后问题(八皇后问题 搜索)1

from collections import defaultdict

n = int(input())
board = []
for i in range(n):
    board.append(input().split())       #n*n棋盘
    
col1 = defaultdict(bool)
dia11 = defaultdict(bool)
dia21 = defaultdict(bool)
col2 = defaultdict(bool)
dia12 = defaultdict(bool)
dia22 = defaultdict(bool)

count = 0


def dfs(index):
    global count
    if n <= 0:
        return 0
    if index == n:

        count += 1
        return
    # 在第index行放置黑皇后
    for j in range(n):
        if not col1[j] and not dia11[index + j] and not dia21[index - j] \
                and board[index][j] == "1":
            col1[j] = True
            dia11[index + j] = True
            dia21[index - j] = True
            board[index][j] = "0"
            for k in range(n):
                if not col2[k] and not dia12[index + k] and not dia22[index - k] \
                        and board[index][k] == "1":
                    col2[k] = True
                    dia12[index + k] = True
                    dia22[index - k] = True
                    board[index][k] = "0"
                    dfs(index + 1)
                    col2[k] = False
                    dia12[index + k] = False
                    dia22[index - k] = False
                    board[index][k] = "1"

            col1[j] = False
            dia11[index + j] = False
            dia21[index - j] = False
            board[index][j] = "1"

    return 0


dfs(0)
print(count)

BASIC-27 2n皇后问题(八皇后问题 搜索)2

from collections import defaultdict

n = int(input())
board = []
for i in range(n):
    board.append(input().split())
col1 = defaultdict(bool)
dia11 = defaultdict(bool)
dia21 = defaultdict(bool)
col2 = defaultdict(bool)
dia12 = defaultdict(bool)
dia22 = defaultdict(bool)
count = 0


def dfs2(index):
    global count
    if index == n:
        count += 1
        return
    # 在第index行放置黑皇后
    for j in range(n):
        if not col2[j] and not dia12[index + j] and not dia22[index - j] \
                and board[index][j] == "1":
            col2[j] = True
            dia12[index + j] = True
            dia22[index - j] = True
            board[index][j] = "0"
            dfs2(index + 1)
            col2[j] = False
            dia12[index + j] = False
            dia22[index - j] = False
            board[index][j] = "1"

    return 0

def dfs1(index):
    if n <= 0:
        return 0
    if index == n:
        dfs2(0)
        return
    # 在第index行放置黑皇后
    for j in range(n):
        if not col1[j] and not dia11[index + j] and not dia21[index - j] \
                and board[index][j] == "1":
            col1[j] = True
            dia11[index + j] = True
            dia21[index - j] = True
            board[index][j] = "0"
            dfs1(index + 1)
            col1[j] = False
            dia11[index + j] = False
            dia21[index - j] = False
            board[index][j] = "1"

    return 0


dfs1(0)
print(count)

BASIC-28 Huffuman树(贪心 Huffuman)

n=int(input())
pi=list(map(int,input().split()))         #输入n个数
Count=[]   #定义最小费用和数组,即每次找到的最小两个数pa,pb求和放入Count列表内
ans=0        #表示最终答案
while len(pi)!=1:
    pi.sort(reverse=True)             #reverse = True 降序, reverse = False 升序(默认)
    pa=pi.pop();pb=pi.pop();
    pi.append(pa+pb)
    Count.append(pa+pb)
for i in range(len(Count)):
    ans+=Count[i]
print(ans)

BASIC-29 高精度加法(数组 高精度)

#解法一:因为python中没有long或者longlong型的数据类型,
#int类型具有无限精度,所以可以直接求解。
#a,b=int(input()),int(input())
#print(a+b)


#解法二
def trans(a):#将输入的字符串变成列表输出
    A=[]
    for i in range(len(a)):
        A.append(eval(a)%10)   #eval() 函数用来执行一个字符串表达式,并返回表达式的值
        a=a[:-1]                         #eval(expression,globals=None, locals=None)返回的是计算结果
    return A                             # (参与计算的python表达式,不为None必须是dictionary对象,不为None可以是任何map对象)          

a=input()

b=input()
A=trans(a)+[0]*(100-len(a))#初始化A,B的长度为100
B=trans(b)+[0]*(100-len(b))
C=[0]*101 #初始化C的长度为101
r=0 #这个是存放的中间变量
for i in range(100):
    o=A[i]+B[i]+r
    C[i]=o%10
    r=o//10
C.reverse()
for i in range(len(C)): #输出结果
    if C[i]!=0:
        for j in C[i:]:
            print(j,end='')
        break

BASIC-30 阶乘计算(高精度)1

#解法一:直接调用math库factorial函数  math.factorial(x):返回x的阶乘
import math
print(math.factorial(int(input())))

BASIC-30 阶乘计算(高精度)2

#解法二:for循环
n=int(input())
ans=1
for i in range(1,n+1):
    ans*=i
print(ans)

BASIC-30 阶乘计算(高精度)3

#解法三:数组存储
n=int(input())
L=[1]#赋初值,不然无法启动
def loop(n):
    global L
    for i in range(len(L)):#正常乘法,每位各自乘
        L[i]*=n
    for i in range(len(L)-1):#进位,但是首位进位不在此列
        L[i+1]+=int(L[i]/10)
        L[i]=L[i]%10
    L1=list(str(L[-1]))#分解首项
    L.pop()#删除首项
    for i in range(len(L1)-1,-1,-1):#倒序归入原序列,注意倒叙!!!
        L.append(int(L1[i]))
for i in range(2,n+1):#n!定义
    loop(i)
for i in range(len(L)):#转化形式
    L[i]=str(L[i])
print(''.join(L[::-1]))#join()输出
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值