2021第二届python蓝桥杯省赛题解(个人版)

暴力大法好!!!

  • 这里所有的所解答的题目,没有经过验证,仅供提供思路(~

A 求余

2021第二届python蓝桥杯省赛题解(个人版)_第1张图片

  • 不用写代码,口算 ,ans=1

B 双阶乘

2021第二届python蓝桥杯省赛题解(个人版)_第2张图片

sum1 = 1
cnt = 1

while(cnt<=3):
    sum1= (sum1 * cnt)%100000
    cnt+=2

print(sum1)

C 格点

2021第二届python蓝桥杯省赛题解(个人版)_第3张图片

nums = 0

for i in range(1,2022):
    for j in range(1,2022):
        if i*j <= 2021:
            nums+=1

print(nums)

D 整数分解

2021第二届python蓝桥杯省赛题解(个人版)_第4张图片

  • 这一题,单纯的暴力,没有那么获取答案,一开始用python写的代码,速度太慢了,由于它是填空题,后面我用的C跑出的答案(时间也不短)
#include 

int main(){
     
	int nums=0;
	int i1,i2,i3,i4,i5;
	
	for(i1=1;i1<2021;i1++){
     
		for(i2=1;i2<2021-i1;i2++){
     
			for(i3=1;i3<2021-i1-i2;i3++){
     
				for(i4=1;i4<2021-i1-i2-i3;i4++){
     
					if((2021-i1-i2-i3-i4)>=1){
     
						nums++;
					}
				}
			}
		}
	}
	printf("%d",nums);
	return 0;
}

E 城邦

2021第二届python蓝桥杯省赛题解(个人版)_第5张图片

def func(i,j):
    strs1 = str(i)
    strs2 = str(j)
    len1 = len(strs1)
    len2 = len(strs2)

    sum1 = 0
    if len1==len2:
        for i in range(1,len1+1):
            cnt = -1*i
            if strs1[cnt]!=strs2[cnt]:
                sum1+=int(strs1[cnt])+int(strs2[cnt])
    elif len1>len2:
        for i in range(1,len2+1):
            cnt = -1*i
            if strs1[cnt]!=strs2[cnt]:
                sum1+=int(strs1[cnt])+int(strs2[cnt])
        for i in range(len2+1,len1+1):
            cnt = -1*i
            sum1+=int(strs1[cnt])
    elif len1<len2:
        for i in range(1,len1+1):
            cnt = -1*i
            if strs1[cnt]!=strs2[cnt]:
                sum1+=int(strs1[cnt])+int(strs2[cnt])
        for i in range(len1+1,len2+1):
            cnt = -1*i
            sum1+=int(strs2[cnt])

    return sum1
        
                

print(func(2021,922))

ans = 0
# 桥之间的遍历

for i in range(1,2021):
    for j in range(i+1,2021):
        ans+=func(i,j)

print(ans)

F 小平方

2021第二届python蓝桥杯省赛题解(个人版)_第6张图片

n = int(input())

n_ban = 1.0*n / 2

ans = 0

for i in range(1,n):
    cnt = (i**2) % n
    if (1.0*cnt)<n_ban:
        ans+=1

print("{:.0f}".format(ans))

G 完全平方数

2021第二届python蓝桥杯省赛题解(个人版)_第7张图片

import math
# 判断一个数是否为完全平方数
def func(num):
    flag = 0
    for i in range(int(math.sqrt(num))+1):
        if i*i == num:
            flag = 1
            break
        if i*i >num:
            break

    if flag == 1:
        return True
    else:
        return False


n = int(input())
ans = 0
for x in range(1,n+1):
    if func(n*x):
        ans = x
        break

print("{:.0f}".format(ans))

H 负载均衡

2021第二届python蓝桥杯省赛题解(个人版)_第8张图片

  • 思路:以时间为顺序点,来模仿操作系统的流程
"""
2(计算机数) 6(任务数)
5 5   (计算机运算能力)
ai(时刻) bi(计算机编号) ci(秒) di(算力)
2 6
5 5
1 1 5 3
2 2 2 6
3 1 2 3
4 1 6 1
5 1 3 3
6 1 3 4

#D  187539689
"""
# 稍微复杂,先过
class PC:
    def __init__(self,id,xingneng):
        self.id = id
        # 性能
        self.xingneng = xingneng
        # 被任务占用的 任务id
        # self.thing_id = []
        # 被占用的性能
        self.useing_xingneng = []
        # 每个任务剩余的时间
        self.shengyv_time = []
        # 计算机剩余的性能
        self.shengyv_xingneng = xingneng
        # 计算机剩余的任务个数
        self.shengyv_renwu = 0

com_nums,thing_nums = map(int,input().split())
com_ablity = [int(i) for i in input().split()]

things = []
for _ in range(thing_nums):
    temp = [int(i) for i in input().split()]
    things.append(temp)

# 初始化PC
PCS = []
for i in range(com_nums):
    pc = PC(i+1,com_ablity[i])
    PCS.append(pc)

now_time = 0
thing_flag = 0

while(thing_flag<thing_nums):
    # 这一秒时候,所有计算机完成情况
    for i in range(com_nums):
        pc = PCS[i]
        if pc.shengyv_renwu != 0:
            for j in range(len(pc.shengyv_time)):
                pc.shengyv_time[j]-=1
                if pc.shengyv_time[j]==0:
                    pc.shengyv_xingneng += pc.useing_xingneng[j]
                    pc.useing_xingneng[j]=0
                    pc.shengyv_renwu-=1
        # 清除 0
        pc.useing_xingneng = [i for i in pc.useing_xingneng if i!=0]
        pc.shengyv_time = [i for i in pc.shengyv_time if i!=0]
        
    if now_time == things[thing_flag][0]:
        bi,ci,di = things[thing_flag][1],things[thing_flag][2],things[thing_flag][3]
        thing_flag+=1
        if di<=PCS[bi-1].shengyv_xingneng:
            PCS[bi-1].useing_xingneng.append(di)
            PCS[bi-1].shengyv_time.append(ci)
            PCS[bi-1].shengyv_xingneng-=di
            PCS[bi-1].shengyv_renwu+=1
            print("{:.0f}".format(PCS[bi-1].shengyv_xingneng))
        else:
            print("-1")

    now_time+=1
        

I 国际象棋

2021第二届python蓝桥杯省赛题解(个人版)_第9张图片

  • 思路:这一题,当时没想到好的算法,我使用的递归来实现的(得不了满分,会超时,如果有好的算法,欢迎留言)
# 判断两点坐标是否相互攻击
def func(p1,p2):
    flag = 0
    if (p1[0]+1)==p2[0] and (p1[1]+2)==p2[1]:
        flag = 1
    elif (p1[0]+1)==p2[0] and (p1[1]-2)==p2[1]:
        flag = 1
    elif (p1[0]-1)==p2[0] and (p1[1]+2)==p2[1]:
        flag = 1
    elif (p1[0]-1)==p2[0] and (p1[1]-2)==p2[1]:
        flag = 1
    elif (p1[0]+2)==p2[0] and (p1[1]+1)==p2[1]:
        flag = 1
    elif (p1[0]+2)==p2[0] and (p1[1]-1)==p2[1]:
        flag = 1
    elif (p1[0]-2)==p2[0] and (p1[1]+1)==p2[1]:
        flag = 1
    elif (p1[0]-2)==p2[0] and (p1[1]-1)==p2[1]:
        flag = 1

    if flag == 1:
        return True
    else:
        return False
    
# 判断坐标  与之前的坐标是否冲突
def zb_and_zbs(zb,zbs):
    if len(zbs)==0:
        return False
    else:
        flag = 0
        for zb2 in zbs:
            if func(zb,zb2):
                flag = 1
                break
        if flag == 1:
            return True
        else:
            return False

# 递归
def dg(zbs,all_zb,K,i):
    global ans
    if len(zbs)==K:
        #print(zbs)
        ans= (ans+1)%1000000007
    elif len(zbs)<K:
        for j in range(i,len(all_zb)):
            one_zb = all_zb[j]
            zbs1 = zbs[:]
            if (one_zb not in zbs) and (not zb_and_zbs(one_zb,zbs)):
                zbs1.append(one_zb)
                dg(zbs1,all_zb,K,j+1)



n,m,K = map(int,input().split())
# 构建所有的坐标
all_zb = []
for i in range(n):
    for j in range(m):
        all_zb.append([i,j])


# 写递归
zbs = []
global ans
ans = 0

for i in range(len(all_zb)):
    zbs = [all_zb[i]]
    dg(zbs,all_zb,K,i+1)

print(ans)       

J

2021第二届python蓝桥杯省赛题解(个人版)_第10张图片

  • 不会,骗分~,嘻嘻

你可能感兴趣的:(python,算法)