...
# 1.統(tǒng)計(jì)工齡
# 給定公司N名員工的工齡,要求按工齡增序輸出每個(gè)工齡段有多少員工。
n=int(input())
lst = list(map(int,input().split()))
d = {}
for x in range(0,n):
? ? if not lst[x] in d:
? ? ? ? d[lst[x]] = 1
? ? else:
? ? ? ? d[lst[x]] = d[lst[x]] + 1
for k in sorted(d.keys()):
? ? print(k,end=':')
? ? print(d[k])
# 2.二分查找
# 利用二分查找找出所給出的數(shù)在數(shù)組中的下標(biāo)
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d = {}
for i in range(n):
? ? d[a[i]] = i
res = []
for i in b:
? ? if i in d:
? ? ? ? res.append(str(d[i]))
print(" ".join(res))
# 3.小陳的社交網(wǎng)絡(luò)
# 小陳近期在總結(jié)自己的朋友圈,通過(guò)一定方法,他知道了某兩個(gè)人是朋友關(guān)系,為了簡(jiǎn)化統(tǒng)計(jì)
n, m = map(int, input().split())
# 初始化并查集數(shù)組,每個(gè)人的父節(jié)點(diǎn)都是自己
fa = [i for i in range(n+1)]
# 查找x的根節(jié)點(diǎn)
def find(x):
? ? if x != fa[x]:
? ? ? ? fa[x] = find(fa[x])
? ? return fa[x]
# 合并x和y所在的朋友圈
def merge(x, y):
? ? fx, fy = find(x), find(y)
? ? if fx != fy:
? ? ? ? fa[fx] = fy
# 處理已知的朋友關(guān)系
for i in range(m):
? ? a, b = map(int, input().split())
? ? merge(a, b)
# 統(tǒng)計(jì)朋友圈個(gè)數(shù),即有多少個(gè)根節(jié)點(diǎn)
cnt = 0
for i in range(1, n+1):
? ? if fa[i] == i:
? ? ? ? cnt += 1
# 輸出小陳至少還需要結(jié)交的朋友數(shù),即朋友圈個(gè)數(shù)-1
print(cnt)
# 4.二叉樹(shù)的遍歷!
# 我們將給出一顆二叉樹(shù),請(qǐng)你輸出它的三種遍歷,分別是先序遍歷,中序遍歷,后序遍歷!
class TreeNode:
? ? def __init__(self, val=0, left=None, right=None):
? ? ? ? self.val = val
? ? ? ? self.left = left
? ? ? ? self.right = right# 遞歸實(shí)現(xiàn)先序遍歷
def preorderTraversal(root):
? ? if not root:
? ? ? ? return []
? ? res = [root.val]
? ? res += preorderTraversal(root.left)
? ? res += preorderTraversal(root.right)
? ? return res# 遞歸實(shí)現(xiàn)中序遍歷
def inorderTraversal(root):
? ? if not root:
? ? ? ? return []
? ? res = inorderTraversal(root.left)
? ? res += [root.val]
? ? res += inorderTraversal(root.right)
? ? return res# 遞歸實(shí)現(xiàn)后序遍歷
def postorderTraversal(root):
? ? if not root:
? ? ? ? return []
? ? res = postorderTraversal(root.left)
? ? res += postorderTraversal(root.right)
? ? res += [root.val]
? ? return res# 構(gòu)建二叉樹(shù)
def buildTree(n, nodes):
? ? tree = {}
? ? for i in range(1, n+1):
? ? ? ? tree[i] = TreeNode(i)
? ? for i in range(n):
? ? ? ? val, left, right = nodes[i]
? ? ? ? if left != -1:
? ? ? ? ? ? tree[val].left = tree[left]
? ? ? ? if right != -1:
? ? ? ? ? ? tree[val].right = tree[right]
? ? return tree[1]
# 主程序
if __name__ == '__main__':
? ? n = int(input())
? ? nodes = [list(map(int, input().split())) for _ in range(n)]
? ? root = buildTree(n, nodes)
? ? print(' '.join(map(str, preorderTraversal(root))))
? ? print(' '.join(map(str, inorderTraversal(root))))
? ? print(' '.join(map(str, postorderTraversal(root))))
# 5.圖深度優(yōu)先遍歷
# 編寫程序?qū)o定的有向圖(不一定連通)進(jìn)行深度優(yōu)先遍歷,圖中包含n個(gè)頂點(diǎn)
n, e = map(int, input().split())
# 構(gòu)建鄰接表
adj = [[] for _ in range(n)]
for i in range(e):
? ? a, b = map(int, input().split())
? ? adj[a].append(b)
# 深度優(yōu)先遍歷函數(shù)
def dfs(v, visited):
? ? visited.add(v)
? ? print(v, end=' ')
? ? for u in sorted(adj[v]):
? ? ? ? if u not in visited:
? ? ? ? ? ? dfs(u, visited)
# 從頂點(diǎn)0開(kāi)始進(jìn)行深度優(yōu)先遍歷
visited = set()
dfs(0, visited)
for i in range(n):
? ? if i not in visited:
? ? ? ? dfs(i, visited)
# 6.旅游規(guī)劃
# 有了一張自駕旅游路線圖,你會(huì)知道城市間的高速公路長(zhǎng)度
import sys
import heapq
N, M, S, D = map(int, sys.stdin.readline().split())
# 構(gòu)建鄰接表
graph = {}
for i in range(M):
? ? u, v, w, f = map(int, sys.stdin.readline().split())
? ? if u not in graph:
? ? ? ? graph[u] = {}
? ? if v not in graph:
? ? ? ? graph[v] = {}
? ? graph[u][v] = (w, f)
? ? graph[v][u] = (w, f)
# Dijkstra算法
dist = {S: 0}
cost = {S: 0}
heap = [(0, S)]
while heap:
? ? d, u = heapq.heappop(heap)
? ? if u == D:
? ? ? ? break
? ? if u in dist and d > dist[u]:
? ? ? ? continue
? ? for v in graph[u]:
? ? ? ? w, f = graph[u][v]
? ? ? ? if v not in dist or d + w < dist[v]:
? ? ? ? ? ? dist[v] = d + w
? ? ? ? ? ? cost[v] = f + cost[u]
? ? ? ? ? ? heapq.heappush(heap, (dist[v], v))
? ? ? ? elif d + w == dist[v] and cost[u] + f < cost[v]:
? ? ? ? ? ? cost[v] = f + cost[u]
? ? ? ? ? ? heapq.heapreplace(heap, (dist[v], v))
print(dist[D], cost[D])
# 7.簡(jiǎn)單計(jì)算器
# 本題要求你為初學(xué)數(shù)據(jù)結(jié)構(gòu)的小伙伴設(shè)計(jì)一款簡(jiǎn)單的利用堆棧執(zhí)行的計(jì)算器。
n = int(input())
nums = list(map(int, input().split()))
ops = input().split()
# 定義堆棧
S1 = []
S2 = []
# 將數(shù)字和運(yùn)算符依次壓入堆棧
for i in range(n):
? ? S1.append(nums[i])
? ? if i < n - 1:
? ? ? ? S2.append(ops[i])
# 循環(huán)執(zhí)行計(jì)算
while S2:
? ? op = S2.pop()
? ? n2 = S1.pop()
? ? n1 = S1.pop()
? ? if op == '+':
? ? ? ? S1.append(n1 + n2)
? ? elif op == '-':
? ? ? ? S1.append(n1 - n2)
? ? elif op == '*':
? ? ? ? S1.append(n1 * n2)
? ? elif op == '/':
? ? ? ? if n2 == 0:
? ? ? ? ? ? print('ERROR: {}/0'.format(n1))
? ? ? ? ? ? exit(0)
? ? ? ? S1.append(n1 // n2)
# 輸出結(jié)果
print(S1[0])
# 8.激光陷阱 1
# 小明想安全通關(guān)一個(gè)100x100網(wǎng)格的激光陷阱。
list1=[[1]*100 for i in range(100)]
n=int(input())
for i in range(n):
? ? x,y=map(int,input().split())
? ? for j in range(100):
? ? ? ? list1[x-1][j]=list1[j][y-1]=0
count=0
for i in list1:
? ? count+=sum(i)
print(count)
# 9.回文素?cái)?shù)
# 小明特別喜歡素?cái)?shù),但是有時(shí)分不清前后,所以請(qǐng)你幫助小明找出給定區(qū)間內(nèi)
def prime(v):
? ? for j in range(2,int(v**0.5)+1):
? ? ? ? if v%j==0:
? ? ? ? ? ? return 0
? ? return 1
A,B=map(int,input().split())
res=[]
for i in range(A,B+1):
? ? c=str(i)
? ? if c==c[::-1 ]and prime(i): # 先判斷回文再判斷素?cái)?shù),因?yàn)榕袛嗷匚牡男时扰袛嗨財(cái)?shù)快O(1)和O(n^0.5)
? ? ? ? res.append(i)
print(len(res))
for i in res:
? ? print(i)
# 10.憤怒的星期一
# 長(zhǎng)壽的小明非常不喜歡星期一,但是如果這一天恰好是小明生日的前一周
import datetime as d
n,y,r=map(int,input().split())
beg,end=d.date(n,y,r),d.date(2022,11,17)
temp,bir=d.timedelta(1),d.timedelta(6)
count=0
while beg<=end:
? ? try: # 異常處理排除非閏年生日是2月29日這一年都沒(méi)有那七天,因?yàn)楫?dāng)為非閏年2月29日,日期錯(cuò)誤d.date(beg.year,y,r)會(huì)報(bào)錯(cuò)
? ? ? ? if beg.isoweekday()==1 and not (d.date(beg.year,y,r)-bir)<=beg<=d.date(beg.year,y,r): # 是星期一且不屬于生日前七天的區(qū)間內(nèi)
? ? ? ? ? ? count+=1
? ? except:
? ? ? ? count+=1
? ? beg+=temp
print(count)
# 11.成績(jī)排序
# 小明成績(jī)很差,一次考試后小明想知道自己的排名是多少,請(qǐng)你幫小明數(shù)一數(shù)。
n,s=map(int,input().split())
res=list(map(int,input().split()))+[s]
res.sort(reverse=True)
print(res.index(s)+1)
# 12.數(shù)的操作
# 小明在玩一個(gè)游戲,對(duì)整數(shù)列表進(jìn)行三種操作。
res=list(map(int,input().split()))
for i in range(int(input())):
? ? temp=input()
? ? if temp[0] in "LR": # 該題頭部插入和尾部插入對(duì)結(jié)果都沒(méi)影響
? ? ? ? res.append(int(temp.split()[-1]))
? ? else:
? ? ? ? a,b,c=temp.split()
? ? ? ? for j in range(len(res)):
? ? ? ? ? ? res[j]=eval(f"{res[j]}{c}") # eval()函數(shù),字符串表達(dá)式的功能
print(sum(res))
# 13.多項(xiàng)式的加減
# 一天數(shù)學(xué)老師想為難小明,給其布置非常多的多項(xiàng)式加減的作業(yè)
dic=[[0]*11 for i in range(1001)] # 使用二維列表存所有未知數(shù)且不同次的系數(shù),外層列表索引代表不同未知數(shù)(下標(biāo)不同),內(nèi)層列表索引代表這個(gè)未知數(shù)的不同次。
for i in range(int(input())):
? ? a,b,c=map(int,input().split())
? ? dic[a][c]+=b
for i in range(int(input())):
? ? a,b,c=map(int,input().split())
? ? dic[a][c]+=b
for index,i in enumerate(dic):
? ? if sum(i)==0: # 如果內(nèi)層列表的和為0代表這一未知數(shù)沒(méi)有出現(xiàn)過(guò)
? ? ? ? continue
? ? print(index,end='')
? ? for j,k in enumerate(i):
? ? ? ? if k!=0:
? ? ? ? ? ? print('',k,j,end='')
? ? print()