# 构造图 # 顶点由'1', '2', '3', ... '40'构成 defbuild_gragh(): graph = dict() with open('network.txt') as f: lines = f.read().splitlines() for vh, line in enumerate(lines, start=1): g = dict() for vs, v in enumerate(line.split(','), start=1): try: g[str(vs)] = int(v) except ValueError: g[str(vs)] = None graph[str(vh)] = g
new_graph = defaultdict(dict) # 只要还有未选中顶点,则循环 while vertes: # 假设最小权重为1000 tmp = 1000 for u in selected: for v in vertes: try: if graph[u][v] != Noneand graph[u][v] < tmp: tmp = graph[u][v] choose_u = u choose_v = v
Three distinct points are plotted at random on a Cartesian plane, for which -1000 ≤ x, y ≤ 1000, such that a triangle is formed.
Consider the following two triangles:
A(-340,495), B(-153,-910), C(835,-947)
X(-175,41), Y(-421,-714), Z(574,-645)
It can be verified that triangle ABC contains the origin, whereas triangle XYZ does not.
Using triangles.txt (right click and ‘Save Link/Target As…’), a 27K text file containing the co-ordinates of one thousand “random” triangles, find the number of triangles for which the interior contains the origin.
NOTE: The first two examples in the file represent the triangles in the example given above.
defmain(): with open('triangles.txt') as f: lines = f.read().splitlines()
counts = 0 for line in lines: points = line.split(',') points = [int(point) for point in points] A = points[:2] B = points[2:4] C = points[4:] P = (0, 0) if area(A, B, C) == area(P, B, C) + area(P, A, C) + area(P, A, B): counts += 1 print(counts)