三维空间中有N个点,每个点可能是三种颜色的其中之一,三种颜色分别是红绿蓝,分别用'R', 'G', 'B'表示。
现在要找出三个点,并组成一个三角形,使得这个三角形的面积最大。
但是三角形必须满足:三个点的颜色要么全部相同,要么全部不同。
首先输入一个正整数N三维坐标系内的点的个数.(N <= 50)
接下来N行,每一行输入 c x y z,c为'R', 'G', 'B' 的其中一个。x,y,z是该点的坐标。(坐标均是0到999之间的整数)
输出一个数表示最大的三角形面积,保留5位小数。
5 R 0 0 0 R 0 4 0 R 0 0 3 G 92 14 7 G 12 16 8
6.00000
import math class Point(object): '''定义三个点''' def __init__(self, c, x, y, z): self.c = c self.x = x self.y = y self.z = z def valid(l1, l2, l3): token = [l1, l2, l3] token.sort() '''判断三个点是否能构成三角形(两边之和大于第三边)''' return token[0] + token[1] > token[2] def cal_len(p1, p2): '''求两点之间的距离''' l = pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2) + pow(p1.z - p2.z, 2) return math.sqrt(l) def cal_s(c1, c2, c3): '''求空间中三个点组成的三角形的面积''' l1 = cal_len(c1, c2) l2 = cal_len(c2, c3) l3 = cal_len(c3, c1) ans = 0 if valid(l1, l2, l3): p = (l1 + l2 + l3) / 2 token = math.sqrt(p * (p - l1) * (p - l2) * (p - l3)) ans = max(token, ans) return ans def solution(N,nums): '''计算三角形的最大面积''' ans=0 for i in range(N): for j in range(i + 1, N): for k in range(j + 1, N): if (nums[i].c == nums[j].c == nums[k].c or len(set([nums[i].c, nums[j].c, nums[k].c])) == 3): '''判断空间中的三个点颜色是否都相同或都不相同''' ans = max(ans, cal_s(nums[i], nums[j], nums[k])) return ans '''主程序''' N = int(input()) nums = [] for i in range(N): c, x, y, z = input().strip().split() nums.append(Point(c, int(x), int(y), int(z))) ans=solution(N,nums) print('{:.5f}'.format(ans))
while True: try: n=input() n=int(n) list1=[] for i in range(n): list1.append(list(input().split(' '))) #print(list1) list2=[] def iscolor(x,y,z):#判断颜色 if x!=y and y!=z and x!=z: return True elif x==y and x==z: return True else: return False def distance(x,y):#计算两点距离 sum1=0 #print(x[1]) #print((int(x[1])-int(y[1]))**2) sum1=((int(x[1])-int(y[1]))**2+(int(x[3])-int(y[3]))**2+(int(x[2])-int(y[2]))**2)**(0.5) return sum1 def issan(x,y,z):#判断是不是三角形 if x<(y+z) and z<(x+y) and y<(x+z): return True else: return False for x in range(n): for y in range(x+1,n): for z in range(y+1,n): x_num=list1[x] y_num=list1[y] z_num=list1[z] if iscolor(x_num[0],y_num[0],z_num[0]): #print('ok') l_xy=distance(x_num,y_num) #print(l_xy) l_xz=distance(x_num,z_num) #print(l_xz) l_yz=distance(y_num,z_num) #print(l_yz) if issan(l_xy,l_xz,l_yz): #print('ok') win=(l_xy+l_xz+l_yz)/2 square=(win*(win-l_xy)*(win-l_xz)*(win-l_yz))**(0.5)#计算面积 list2.append(square) #print(list2) max_num=max(list2) #print(max_num) #float('%.2f' % max_num) print('{:.5f}'.format(max_num)) #print(float('%.6f' % max_num)) except: break
参考大佬的算法写的Python代码
#!/usr/bin/env python # -*- coding: utf-8 -*- import math n=int(raw_input()) i=0 ind=[] while i<n: ind.append([a for a in raw_input().split()]) i+=1 RED=[] GREEN=[] BLUE=[] square=[] for i in ind: if i[0]=='R': RED.append([int(o) for o in i[1:]]) elif i[0]=='G': GREEN.append([int(o) for o in i[1:]]) else: BLUE.append([int(o) for o in i[1:]]) def calSquare(l1,l2,l3): half = (l1 + l2 + l3) / 2.0 return math.sqrt(half * (half - l1) * (half - l2) * (half - l3)) for item in [RED,GREEN,BLUE]: if len(item)>2: for i in range(len(item)-2): # item[i] for j in range(i+1,len(item)-1): # item[j] pq = [item[i][z]-item[j][z] for z in range(3)] l1 = math.sqrt(sum([pq[z]*pq[z] for z in range(3)])) for k in range(i+2,len(item)): # item[k] pr = [item[i][z]-item[k][z] for z in range(3)] qr = [item[j][z] - item[k][z] for z in range(3)] l2 = math.sqrt(sum([pr[z]*pr[z] for z in range(3)])) l3 = math.sqrt(sum([qr[z]*qr[z] for z in range(3)])) if l1 + l2 != l3 or l2 + l3 != l1 or l1 + l3 != l2: s = calSquare(l1, l2, l3) square.append(s) if len(RED)>2 and len(GREEN)>2 and len(BLUE)>2: for p in RED: for q in GREEN: pq = [p[i] - q[i] for i in range(3)] l1 = math.sqrt(sum([pq[z] * pq[z] for z in range(3)])) for r in BLUE: pr = [p[i] - r[i] for i in range(3)] qr = [q[z] - r[z] for z in range(3)] l2 = math.sqrt(sum([pr[z] * pr[z] for z in range(3)])) l3 = math.sqrt(sum([qr[z] * qr[z] for z in range(3)])) if l1+l2!=l3 or l2+l3!=l1 or l1+l3!=l2: s=calSquare(l1,l2,l3) square.append(s) print min(square)不知道为啥,他给的那个例子输出是6.00000,我的输出是6.0,就通不过