第一题:开锁——数学期望
def openLockTime(n, m, time):
time_reverse = []
for i in range(m):
m_time = []
for j in range(n):
m_time.append(time[j][i])
time_reverse.append(sorted(m_time))
E = 0.000000
p = 1 / n
for i in range(m):
i_sum = 0
for j in range(n):
i_sum = i_sum + sum(time_reverse[i][:j + 1])
E += p * i_sum
return E
n, m = list(map(int, input().split()))
time = []
for i in range(n):
t = list(map(int, input().split()))
time.append(t)
print(openLockTime(n,m,time))
'''
2 4
347 177 40 84
107 282 347 193
'''
n, m = list(map(int, input().split()))
time = []
for i in range(n):
t = list(map(int, input().split()))
time.append(t)
第二题:马夫**链——条件概率(矩阵相乘)
def predictOwnP(step, last, P):
for i in range(step):
stab = 0.0
Own = 0.0
Fail = 0.0
for j in range(3):
stab += P[0][j] * last[j]
Fail += P[1][j] * last[j]
Own += P[2][j] * last[j]
last = [stab, Fail, Own]
if last[-1] > 0.5:
return 1
else:
return 0
group_num = int(input())
for i in range(group_num):
step = int(input())
first = list(map(float, input().split()))
P = []
for _ in range(3):
P.append(list(map(float, input().split())))
print(predictOwnP(step, first, P))
'''
2
2
0.4 0.2 0.4
0.2 0.1 0.7
0.3 0.5 0.2
0.5 0.4 0.1
10
0.4 0.2 0.4
0.2 0.1 0.7
0.3 0.5 0.2
0.5 0.4 0.1
'''
第三题:迎宾车队——极差(双指针)
def carMaxNum(n,speed):
speed = sorted(speed)
maxNum = 0
left = 0
right = 0
while right < n:
if speed[right] - speed[left] <= 10:
maxNum = max(maxNum,right - left + 1)
right += 1
else:
left += 1
return maxNum
n = int(input())
speed = list(map(int,input().split()))
print(carMaxNum(n,speed))
'''
6
17 3 21 8 44 24
'''
'''
3
9 7 4
'''
第四题:水站的水流量——直接找的规律
def calcuMaxNode(n, t):
maxT = sum([i+1 for i in range(n)])
if t >= maxT:
return maxT
time = [1 for _ in range(11)]
for i in range(2,11):
time[i] = time[i-1] + i
print(time)
layers = 1
for layer in range(1,n+1):
if time[layer] > t:
layers = layer
break
inside_time = layers/(layers - 1)
outside_time = 2*layers/(layers - 1)
if time[layers-1] + inside_time*(layers-2) > t:
return sum([i for i in range(1,layers)])
elif time[layers-1] + inside_time*(layers-2) <= t and time[layers] + outside_time*2 > t:
return sum([i for i in range(1,layers+1)]) - 2
else:
return sum([i for i in range(1,layers+1)])
n, t = list(map(int, input().split()))
print(calcuMaxNode(n,t))
'''
3 5
'''
第五题:轰炸——DFS或者BFS(将1包围的0改为2)
def hongzha(matrix):
m = len(matrix)
n = len(matrix[0])
def judge(indexi,indexj,hashtable,matrix):
if indexi < 0 or indexj < 0 or indexi >= m or indexj >= n:
return False
if hashtable[indexi][indexj] == True:
return False
if matrix[indexi][indexj] == 1:
return False
return True
def bfs(indexi,indexj,hashtable,res):
'''
:param indexi:
:param indexj:
:param hashtable:
:param P: 存放着四边上的0元素坐标,后面会将不被包围的坐标也添加上去
:return:
'''
P= [(indexi,indexj)]
hashtable[indexi][indexj] = True
I = [0,-1,1,0]
J = [-1,0,0,1]
while P:
indexi,indexj = P[0]
res.append(P[0])
for i in range(4):
if judge(indexi+I[i],indexj+J[i],hashtable,matrix):
P.append((indexi+I[i],indexj+J[i]))
hashtable[indexi+I[i]][indexj+J[i]] = True
P = P[1:]
hashtable = [[False for _ in range(n)] for _ in range(m)]
res = []
for i in range(m):
if judge(i,0,hashtable,matrix):
bfs(i,0,hashtable,res)
if judge(i,n-1,hashtable,matrix):
bfs(i, n-1, hashtable, res)
for j in range(n):
if judge(0,j,hashtable,matrix):
bfs(0,j,hashtable,res)
if judge(m-1,j,hashtable,matrix):
bfs(m-1,j, hashtable, res)
for i in range(m):
for j in range(n):
if matrix[i][j] == 0 and (i,j) not in res:
matrix[i][j] = 2
return matrix
import random
m = random.randint(5,11)
n = random.randint(5,11)
matrix = []
for i in range(m):
line = []
for j in range(n):
line.append(random.randint(0,1))
matrix.append(line)
newMatrix = hongzha(matrix)
for line in newMatrix:
print(line)
|