# HNUCM-2024年春季学期《算法分析与设计》练习14

3 篇文章 0 订阅
3 篇文章 0 订阅
3 篇文章 0 订阅

#### 题目描述

5

2

while True:
n=int(input())
m=bin(n)
a=list(m)
number=0
for i in a:
if i=='1':
number+=1
print(num)

#### 题目描述

2

7844

9121

Case 1: 2

Case 2: 0

def check(x: int, y: int):
tmp = 0
while x > 0:
tmp = tmp + 1 if x % 10 != y % 10 else tmp
x, y = x // 10, y // 10
if tmp == 1:
return 1
else:
return 0
t, cnt = int(input()), 0
while t> cnt:
cnt, n, res = cnt + 1, int(input()), 0
for i in range(32, 100):
res = res + check(i ** 2, n)
print(f"Case {cnt}: {res}")

#### 题目描述

2

3

.X.

11

...XX....XX

1

3

t=int(input())
while t>0:
n=int(input())
s=str(input())
s=list(s)
count,i=0,0
while i<n:
if s[i]=='.':
count+=1
i+=3
else:
i+=1
print(count)
t-=1

#### 题目描述

5 7

0 1 10

0 3 30

0 4 100

1 2 50

2 4 10

3 2 20

3 4 60

0 10 50 30 60

while True:
n,m=map(int,input().split())
k=[[float("inf") for i in range(n)] for j in range(n)]
for i in range(m):
a,b,c=map(int,input().split())
k[a][b]=c
d=[float("inf") for i in range(n)]
d[0]=0
t=0
u=[0 for i in range(n)]
for i in range(n):
maxlen=float("inf")
for j in range(n):
if u[j]!=1 and d[j]<maxlen:
maxlen=d[j]
t=j
u[t]=1
for i in range(n1):
if u[i]!=1 and d[t]+k[t][i]<d[i]:
d[i]=d[t]+k[t][i]
for x in d:
print(x,end=" ")
print()

#### 题目描述

s=[int(i) for i in range(1,10)]
a=[False]*9
def dfs(step):
if step==9:
if s[0]*100+s[1]*10+s[2]+s[3]*100+s[4]*10+s[5]==s[6]*100+s[7]*10+s[8]:
x=s[0]*100+s[1]*10+s[2]
y=s[3]*100+s[4]*10+s[5]
z=s[6]*100+s[7]*10+s[8]
print(f"{x}+{y}={z}")
for j in range(1,10):
if a[j-1]==False:
s[step]=j
a[j-1]=True
dfs(step+1)
a[j-1]=False
dfs(0)

#### 题目描述

1<=n,m<=100

5 5

****@

*@@*@

*@**@

@@@*@

@@**@

2

import sys
sys.setrecursionlimit(100000)
def slove(r,c,id):
if r<0 or r>=m or c<0 or c>=n:
return
if idx[r][c]>0 or pic[r][c]!='@':
return
idx[r][c]=id
for dr in range(-1,2):
for dc in range(-1,2):
if dr!=0 or dc!=0:
slove(r+dr,c+dc,id)
while True:
m,n=map(int,input().split())
idx=[[0]*n for _ in range(m)]
pic=[]
for i in range(m):
pic.append(list(input()))
cnt=0
for i in range(m):
for j in range(n):
if idx[i][j]==0 and pic[i][j]=='@':
cnt+=1
slove(i,j,cnt)

print(cnt)

#### 题目描述

1<=n,m<=10

5 5

0 1 1 1 1

0 0 0 0 1

1 1 1 0 1

0 0 0 0 1

0 1 1 0 0

2 1 1 1 1

2 2 2 2 1

1 1 1 2 1

0 0 0 2 1

0 1 1 2 2

def dfs(x, y):
if x == n - 1 and y == m - 1:
paths.append(path[:])
return
else:
for t in d:
fx, fy = x + t[0], y + t[1]
if 0 <= fx < n and 0 <= fy < m and maze[fx][fy] == 0:
maze[fx][fy]= 2
path.append((fx, fy))
dfs(fx, fy)
maze[fx][fy]= 0
path.pop()
return

d = [(-1, 0), (1, 0), (0, -1), (0, 1)]
while True:
try:
n, m = map(int, input().split())
maze, paths, path = [[int(i) for i in input().split()] for _ in range(n)], [], [(0, 0)]
dfs(0, 0)
for i, j in paths[0]:
maze[i][j] = 2
for i in range(n):
for j in range(m):
if j!= 0:
print('', maze[i][j], end='')
else:
print(maze[i][j], end='')
print()
except:
break

#### 题目描述

3 3

1 2 5 5

1 3 8 11

2 3 3 5

8 10

def dijkstra(s: int, t: int):
visited, co, ti = [False] * (n + 1), [0] * (n + 1), [0] * (n + 1)
for i in range(1, n + 1):
co[i], ti[i] = co2[s][i], tim[s][i]
co[s], ti[s], visited[s] = 0, 0, True
for i in range(n - 1):
index, min_co2, min_time = 0, float('inf'), float('inf')
for j in range(1, n + 1):
if not visited[j] and (co[j] < min_co2 or (co[j] == min_co2 and ti[j] < min_time)):
min_co2, min_time, index = co[j], ti[j], j
visited[index] = True
for v in range(1, n + 1):
tmp_co2, tmp_time = co[index] + co2[index][v], ti[index] + tim[index][v]
if not visited[v] and (tmp_co2 < co[v] or (tmp_co2 == co[v] and tmp_time < ti[v])):
co[v], ti[v] = tmp_co2, tmp_time
print(co[t], ti[t])

n, m = map(int, input().split())
co2, tim = [[float('inf')] * (n + 1) for _ in range(n + 1)], [[float('inf')] * (n + 1) for _ in range(n + 1)]
for _ in range(m):
a, b, c, d = map(int, input().split())
co2[a][b] = co2[b][a] = min(c, co2[a][b])
tim[a][b] = tim[b][a] = min(d, tim[a][b])
dijkstra(1, n)

06-14 1892
06-09 730
06-18 2307
04-08 1053
09-12 357
09-19 586
09-18 250

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