阿里巴巴模拟笔试编程题

将军大胜归来, 夺取很多城堡(xi, yi)。国王向将军许诺, 你站在任意的城堡上, 选择任意角度,看得见的城堡都是你的,包括你站的城堡,但头不能动但。而且不能站在城堡构成的凸集点上。 将军的视角刚好小于180度(无限接近180度), 可以看得无限远。 请帮忙计算出将军最多能得到多少城堡。如果所有的城堡都在凸集点上,那么将军一个城堡也得不到。

输入范例:
10 1 1 -1 1 -1 -1 1 -1 0 0
输出范例:
3

题目都读不懂啊……有人解答下不
全部评论
/** * Created by MKD on 2017/8/21. * 思路是不断的利用两个点连线,获取能够取得的最大点,并排除凸点情况 */ import java.util.*; public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); int res; int _points_size = 0; _points_size = Integer.parseInt(in.nextLine().trim()); double[] _points = new double[_points_size]; double _points_item; for(int _points_i = 0; _points_i < _points_size; _points_i++) { _points_item = Double.parseDouble(in.nextLine().trim()); _points[_points_i] = _points_item; } res = castle(_points); System.out.println(String.valueOf(res)); } static int castle(double[] points) { int len = points.length; int result = 0; if(len<=8) return 0; else{ for (int j=0;j<len/2 ;j++){ double x1 = points[2*j]; double y1 = points[2*j+1]; for(int k=0;k<len/2;k++){ if(k==j) continue; double x2 = points[2*k]; double y2 = points[2*k+1]; double w= getw(x1,y1,x2,y2); double b = getb(x1,y1,x2,y2); int r= getMax(j,k,len,w,b,points); if(r>result) result=r; } } } return result; } public static Double getw(double x1, double y1, double x2, double y2){//计算w double w = (x1-x2)/(y1-y2); return w; } public static Double getb(double x1, double y1, double x2, double y2){//计算b double w = (x1-x2)/(y1-y2); double b = y1 - x1*w; return b; } public static int getMax(int j,int k,int len,double w,double b,double[] points){//寻找最多的城堡 int n0=0;int n1=0;int n2=0; int result=0; for(int i=0;i<len/2;i++) { if (i == j || i == k) continue; double x = points[2 * i]; double y = points[2 * i + 1]; double re = y - w * x + b; if (re > 0) n1 = n1 + 1; else if (re < 0) n2 = n2 + 1; else n0 = n0 + 1; } result = Math.max((n0+n1),(n2+n0)); if(result == len/2 - 2) return 0;//如果加上连线上除开连接的两个点,数量为total-2,那么一定是凸点 else return result+1;//排除凸点后,总数再加上本身点,固 + 1 } }
点赞 回复 分享
发布于 2017-08-21 22:00
看不懂。
点赞 回复 分享
发布于 2017-08-21 19:38
【森林举行运动会,小伙伴们身上每个都印着一个字符标记,排成一列,***会要挑出每列里相邻小伙伴身上没有重复字符标记的,最多能挑出几个? 比如:小伙伴们的字符标记串起来是“ccccccbc” 那相邻的小伙伴身上没有重复的字符标记是cb或者bc,那这个人数就是2】 输入范例: “ccccccbc” 输出范例: 2 这个题这么简单。。为啥就是通不过。。求问
点赞 回复 分享
发布于 2017-08-21 19:39
给你们感受下当时我的思路
点赞 回复 分享
发布于 2017-08-21 19:56
摄像老是弹出来好烦
点赞 回复 分享
发布于 2017-08-21 19:57
我的是猴子偷桃
点赞 回复 分享
发布于 2017-08-21 19:58
难道说打草稿都不行吗,,总是弹出来,,一低头就弹出来,,
点赞 回复 分享
发布于 2017-08-21 19:58
我上次也是这个题
点赞 回复 分享
发布于 2017-08-21 20:00
1.判断是否有非凸集点, (1)遍历每一点,其余点逆时针排序。http://www.cnblogs.com/dwdxdy/p/3230156.html (2)判断那个点是凸集点不?就是看逆序的点集构成的连续向量序列,看那个点是否都在向量序列的同一边。同一边则为非凸集点,否则为凸集点。 2.对于非凸集点的情况,再找出最多城堡的数目。 不知道思路对不对?
点赞 回复 分享
发布于 2017-08-21 20:02
不能站在城堡构成的凸集点上,这句话怎么理解
点赞 回复 分享
发布于 2017-08-21 20:07

相关推荐

03-14 11:58
门头沟学院 Java
腾讯暑期实习java选手,完全不懂C++,貌似游戏行业都是用C++的而且天美好像在成都,个人比较想去上海或深圳
siestaaaaaa:天美不止在成都,深圳上海都有。 游戏服务器也不全是cpp,至少我们项目是java ,但是工作中什么语言都会用到,比如cpp、lua、py等等,语言本身其实不重要
点赞 评论 收藏
分享
云边有个小卖铺儿:校招生违约率低,所以我要高😂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务