第十届蓝桥杯-省赛-Java-A组-python代码

A


def judge(num):
    num=str(num)
    if "2" in num or "0" in num or "1" in num or "9" in num:
        return True
    return False
ans=0
for i in range(1,2020):
    if judge(i):
        ans+=i**2
print(ans)
#2658417853

B

f1=1
f2=1
f3=1
for i in range(20190321):
    f4=f1+f2+f3
    f1=f2
    f2=f3
    f3=f4%10000
print(f3)
#4659

C

"""
测试数据
30 50 
01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000
"""
global mp,n
global visit,loc
global tag
loc=["D","L","R","U"]
def judge(x,y):
    if 0<=x<=29 and 0<=y<=49:
        return True
    return False

def bfs():
    queue=[]
    queue.append([0,0])

    while len(queue)!=0:
        front=queue.pop(0)
        x=front[0]
        y=front[1]
        if x==29 and y==49:
            break
        if judge(x+1,y) and mp[x+1][y]=="0":
            if not visit[x+1][y]:
                tag[x+1][y]="D"
                visit[x+1][y]=1
                queue.append([x+1,y])
        if judge(x,y-1) and mp[x][y-1]=="0":
            if not visit[x][y-1]:
                tag[x][y-1]="L"
                visit[x][y-1]=1
                queue.append([x,y-1])
        if judge(x,y+1) and mp[x][y+1]=="0":
            if not visit[x][y+1]:
                tag[x][y+1]="R"
                visit[x][y+1]=1
                queue.append([x,y+1])
        if judge(x-1,y) and mp[x-1][y]=="0":
            if not visit[x-1][y]:
                tag[x-1][y]="U"
                visit[x-1][y]=1
                queue.append([x-1,y])
def output():
    x=29
    y=49
    load=[]
    while x!=0 or y!=0:
        load.insert(0,tag[x][y])
        
        if tag[x][y]=="U":
            x+=1
        elif tag[x][y]=="D":
            x-=1
        elif tag[x][y]=="L":
            y+=1
        elif tag[x][y]=="R":
            y-=1
    print("".join(load))
n=30
mp=[""]*30
visit=[[ 0 for i in range(50)]for j in range(30)]
tag=[[ 0 for i in range(50)]for j in range(30)]
for i in range(n):
    mp[i]=input()
bfs()
output()
"""
DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUUR
UUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLL
DDLLLDLDDDLDDRRRRRRRRRDDDDDDRR
"""


D

"""
假设最大值为27
那么包含27的本周需要3个大于27的数
其余3周需要4个大于27的数
也就是需要15个大于27的数,可行

按照规律,最大值为34
"""


E

import math

global n,d,c

#欧拉筛时间复杂度过大
def EulerFilter():
    size=int(math.sqrt(n))+1

    visit=[0]*(size+1)
    prime=[]
    for i in range(2,size+1):
        if visit[i]==0:
            prime.append(i)
        for j in range(len(prime)):
            if i*prime[j]>size:
                break
            visit[i*prime[j]]=1
            if i%prime[j]==0:
                break
    return visit

def is_prime(num):
    for i in range(2,int(math.sqrt(num))+1):
        if num%i==0:
            return False
    return True

def get_pq():

    #p=891234941 q=1123984201
    size=int(math.sqrt(n))+1
    for i in range(size,-1,-1):
        if n%i==0:
            if is_prime(i):
                p=i
                q=n//p
                break
    return p,q

#欧几里得扩展寻找逆元
global x,y
x=[1]
y=[0]
def extend(a,b):
    if b==0:
        
        return
    extend(b,a%b)
    tx=x[0]
    ty=y[0]
    x[0]=ty
    y[0]=tx-(a//b)*ty
    
#快速幂
def fast_pow(a,b):
    ans=1
    while b:
        if b&1:
            ans=(ans*b)%n
        a=(a**2)%n
        b>>=1
    return ans
n=1001733993063167141
d=212353
c=20190324
#########
p=891234941
q=1123984201
#########
e=823816093931522017
#########
ans=fast_pow(c,e)
print(ans)
##579706994112328949

F

#15
n=int(input())
msg=list(map(int,input().split()))
msg.insert(0,0)

value=[0]*100

deep=1
flag=0
while True:
    begin=2**(deep-1)
    end=2**deep-1
    if end>=n:
        flag=1
    ans=0
    for i in range(begin,min(end+1,n+1)):
        ans+=msg[i]
    value[deep]=ans
    deep+=1
    if flag:
        break
maxnum=max(value)
for i in range(1,100):
    if value[i]==maxnum:
        print(i)
        break

G

"""
2 6 6
1 1
5 2
3 1
6 2
2 1
6 2

"""
#20
n,m,t=map(int,input().split())
msg=[[] for i in range(100010)]

cache=[0]*(n+1)
pro=[0]*(n+1)

can_sub=set()
for i in range(m):
    time,d=map(int,input().split())
    msg[time].append(d)
for i in range(1,t+1):
    nosub=set()
    for j in range(len(msg[i])):
        
        idn=msg[i][j]
        pro[idn]+=2
        can_sub.add(idn)
        nosub.add(idn)
        if pro[idn]>5:
            cache[idn]=1
    remove=[]
    for s in can_sub:
        if s not in nosub:
            pro[s]-=1
            if pro[s]==0:
                remove.append(s)
            elif pro[s]==3:
                cache[s]=0
    for j in range(len(remove)):
        can_sub.remove(remove[j])
print(cache.count(1))

H
python过不了,写了C++

"""
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1100010;

int n;
int a[N];

int find(int x)                                 // 寻找父节点 + 路径压缩
{
    if(a[x] != x) a[x] = find(a[x]);            // 如果不指向父节点
    return a[x];
}

int main()
{
    for (int i = 1; i <= N; i ++) a[i] = i;     // 初始化,自己是自己的父节点
    
    cin >> n;
    for (int i = 1; i <= n; i ++)
    {
        int x;
        scanf("%d", &x);
        x = find(x);                            // 找到 x 指向的父节点
        printf("%d ", x);
        a[x] = x + 1;                           // 将 x 的父节点更新为 x+1
    }
    
    return 0;
}

"""
def find(x,arr):
    while arr[x]!=-1:
        x=arr[x]
    return x

n=int(input())
msg=list(map(int,input().split()))

arr=[-1]*1000010
for i in range(n):
    num=find(msg[i],arr)
    print(num,end="")
    arr[num]=num+1
    if i!=n-1:
        print(" ",end="")

I

#include <bits/stdc++.h>
using namespace std;
int a[105],dp[(1<<20)+10];
int n,m,k;
int x;
int main(){
	cin>>n>>m>>k;
	memset(dp,-1,sizeof(dp));
	for(int i=1;i<=n;i++){
		int s=0;
		//记录一包糖果代表的二进制数
		for(int j=1;j<=k;j++){
			cin>>x;
			s|=(1<<(x-1));
		}
		dp[s]=1;
		a[i]=s;
	}
	for(int i=1;i<=n;i++){
		for(int j=0;j<(1<<m);j++){
			if(dp[j]==-1)
				continue;
			if(dp[a[i]|j]!=-1){
				dp[a[i]|j]=min(dp[a[i]|j],dp[j]+1);
			}
			else{
				dp[a[i]|j]=dp[j]+1;
			}
		}
	}
	cout<<dp[(1<<m)-1];
	return 0;
}

J
我不会

上一篇:100 个 Python 小例子(练习题)


下一篇:mysql – 如何编写迁移以使用ManyToManyField更改模型的主键