昨天搜狗的一道题。。。。。(反正我挂了)
#!/bin/python # -*- coding: UTF-8 -*- import sys """搜狗题目,圆周上两点距离, 第一行:输入n(圆上的n个点) 第二行:输入n个点的度数s(0<=s<=360) 双精度,8位小数 输出园中距离最远的两点之间的夹角度数m(0<=m<=180),双精度,保留8位小数 """ n = input() circle = [] for i in range(n): doublenum = sys.stdin.readline().strip() doublenum = float(doublenum) circle.append(doublenum) """二分查找寻找下届,恰好小于key""" def Binery_Find(circle,n,key): low = 0 high = n-1 mid = (low + high) / 2 while low < high: if circle[mid]< key : low = mid else: high = mid -1 mid = (low + high+1) / 2 return mid maxx = 0 for i in range(n-1): key = circle[i]+180 if key > 360: key -= 360 mid1 = Binery_Find(circle,n,key) mid2 = mid1+1 if mid1< n-1: maxx = max(circle[mid1]-circle[i],maxx,360-(circle[mid2]-circle[i])) else: maxx = max((circle[mid1] - circle[i]), maxx) print "%.8f"%maxx
笔试的时候只会用暴力,这个是结束之后讨论后写的代码,还有什么不足的求赐教!!!