Just Do IT !

Python算法学习: 竞码编程-蓝桥杯校内选拔赛(初赛)重现赛

字数统计: 640阅读时长: 3 min
2020/03/09 Share

oj网站地址:竞码编程-蓝桥杯校内选拔赛(初赛)重现赛
@[TOC]

试题A:第N个素数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def isPrime(n):
if n == 2:
return True
for i in range(2, n):
if n % i == 0:
return False
return True


count = 0
for i in range(2, 10000):
if isPrime(i):
count += 1
if count == 300:
print(i)
print(count)
break

试题B:coffee的签到题

如果n是偶数,则无法全部进行染色,如果是奇数,则可以全部进行染色

1
2
3
4
5
6
n = int(input())

if n % 2 == 0:
print("no")
else:
print("yes")

试题C:不一样的日期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
YY,MM,DD,dd = map(int,input().split())
if dd > 0:
for i in range(1,dd+1):
DD += 1
if DD > 13:
MM += 1
DD = 1
if MM > 23:
YY += 1
MM = 1
else:
for i in range(1,abs(dd)+1):
DD -= 1
if DD <= 0:
MM -= 1
DD = 13
if MM <= 0:
YY -= 1
MM = 23
print(YY,MM,DD)

试题D:三位分节法

1
2
3
4
5
6
7
8
9
10
11
12
str = input()
str = str[::-1] # 字符串翻转
ans = ""
cnt = 0
for i in range(len(str)):
if cnt == 3:
ans = ans + ","
cnt = 0
cnt += 1
ans = ans + str[i]
ans = ans[::-1]
print(ans)

试题E:水坑题

1
2
3
a,b = map(int, input().split())
ans = (a*b) % (pow(10,9)+7)
print(ans)

试题F:站队

Python部分数据超时

1
2
3
4
5
6
7
8
9
10
11
12
13
n = int(input())
data = []
for i in range(1, n+1):
tmp = list(map(int, input().split()))
tmp.append(i)
data.append(tmp)

data = sorted(data,key=lambda x:x[1])
data = sorted(data,key=lambda x:x[0])

for i in range(n-1):
print(data[i][2], end=" ")
print(data[n-1][2])

试题G:冷门进制

试题H:拯救阿拉德大陆

1
2
3
4
5
6
7
8
9
10
11
12
n = int(input())
nums = list(map(int, input().split()))
ans = 0

for i in range(1,n+1):
for num in nums:
if i % num == 0:
ans += 1
break
else:
continue
print(ans)

试题J:馋嘴羊

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import sys
sys.setrecursionlimit(1000000) # 限制递归深度为

def dfs(x, y):
if x < 0 or y < 0 or x > n - 1 or y > m - 1 or vis[x][y] == 1 or arr[x][y] == '0':
return
vis[x][y] = 1
global ans
ans += 1
dfs(x - 1, y)
dfs(x + 1, y)
dfs(x, y + 1)
dfs(x, y - 1)

if __name__ == '__main__':
n, m ,x, y= map(int, input().split())

arr = []
for i in range(n):
str = input()
arr.append(list(str))

vis = [[0 for _ in range(m)] for _ in range(n)] # 判断是否走过,0为未走过,1为走过
ans = 0

dfs(x,y)


print(ans)

连通块问题

题目地址:http://oj.hzjingma.com/p/29?view=classic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def dfs(grid, i, j):
if not 0 <= i < len(grid) or not 0 <= j < len(grid[0]) or grid[i][j] == '.': return
grid[i][j] = '.'
dfs(grid, i + 1, j)
dfs(grid, i, j + 1)
dfs(grid, i - 1, j)
dfs(grid, i, j - 1)
dfs(grid, i + 1, j + 1)
dfs(grid, i - 1, j + 1)
dfs(grid, i - 1, j - 1)
dfs(grid, i + 1, j - 1)

if __name__ == '__main__':
n, m = map(int, input().split())

arr = []
for i in range(n):
str = input()
arr.append(list(str))

ans = 0
for i in range(n):
for j in range(m):
if arr[i][j] == 'W':
dfs(arr,i, j)
ans += 1
print(ans)

CATALOG
  1. 1. 试题A:第N个素数
  2. 2. 试题B:coffee的签到题
  3. 3. 试题C:不一样的日期
  4. 4. 试题D:三位分节法
  5. 5. 试题E:水坑题
  6. 6. 试题F:站队
  7. 7. 试题G:冷门进制
  8. 8. 试题H:拯救阿拉德大陆
  9. 9. 试题J:馋嘴羊
  10. 10. 连通块问题