京东算法岗考了的一道算法题
另一个博客上有个详细的描述:http://blog.csdn.net/snowy_smile/article/details/50131317
内容:
自己用的python写的,放上来python代码。
import math
def corner(x,y,o_x,o_y,r):
#sin value
dis=math.sqrt((x-o_x)**2 + (y-o_y)**2)
return r/dis
def cost_func(ang):
#cost function, ang is a three dimension vector
result=0
for i in range(3):
result+=(ang[i]-ang[(i+1)%3])**2
return result
def cal_cost(x,y,three_point):
ang=[]
for i in range(3):
cor=corner(x,y,three_point[i][0],three_point[i][1],three_point[i][2])
ang.append(cor)
return cost_func(ang)
flag = 0
while 1:
if flag==1:
break
#save data(x,y,r)
input_list=[]
for _ in range(3):
s = raw_input().split()
if s==[]:
flag=1
break
x,y,r = int(s[0]),int(s[1]),int(s[2])
input_list.append((x,y,r))
#init
center_x = sum([x[0] for x in input_list])/3
center_y = sum([x[1] for x in input_list])/3
evr = cal_cost(center_x,center_y,input_list)
#move vector
dx = (-1,0,1,0)
dy = (0,-1,0,1)
step = 1
for _ in xrange(10**6):
for i in range(4):
x = center_x + step * dx[i]
y = center_y + step * dy[i]
new_cost=cal_cost(x,y,input_list)
if new_cost< evr:
center_x=x
center_y=y
evr=new_cost
break
else:
step=step/2
if step<= 10**(-6):
break
print center_x,center_y