题目及题目来源

链接:https://www.nowcoder.com/questionTerminal/bfc691e0100441cdb8ec153f32540be2
来源:牛客网 首页 > 试题广场 > max-points-on-a-line
[编程题]max-points-on-a-line
热度指数:67696 时间限制:1秒 空间限制:32768K
算法知识视频讲解 Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

题解 及完整类

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
int high=0; //++high mode
struct Point {
int x;
int y;
Point() : x(0), y(0) {}
Point(int a, int b) : x(a), y(b) {}
};
/* 链接:https://www.nowcoder.com/questionTerminal/bfc691e0100441cdb8ec153f32540be2
来源:牛客网 需要两重循环,第一重循环遍历起始点a,第二重循环遍历剩余点b。 a和b如果不重合,就可以确定一条直线。 对于每个点a,构建 斜率->点数 的map。 (1)b与a重合,以a起始的所有直线点数+1 (用dup统一相加) (2)b与a不重合,a与b确定的直线点数+1
*/ class Solution {
public:
int maxPoints(vector<Point> &points) {
int size=points.size(); if(size==0)return 0;
else if(size==1)return 1;
else if(size==2) return 2;
int ret=0; for(int i=0;i<size;i++){ //遍历起点a map<double,int>mp; //构建斜率->点数的map集合
int vcent=0; //垂直的点
int dub=0; //重合的点 for(int j=0;j<size;j++){ //枚举起点b
if(i==j)continue; //确定x,y的差值: x1,x2
double x1 = points[i].x-points[j].x;
double y1 = points[i].y-points[j].y; //重合的情况
if(x1==0&&y1==0){
dub++;
ret=max(ret,dub+1);
} //垂直线上的情况
else if(x1==0){
vcent++;
ret=max(ret,1+dub+vcent);
}
// 求出斜率,保存到map中
else{
double k=y1/x1;
mp[k]++;
ret=max(ret,1+dub+mp[k]);
} // cout<<"i= "<<i<<" j="<<j<<" ret="<<ret;
// printf(" dub=%d vent=%d\n",dub,vcent);
}
// cout<<endl;
}
return ret;
}
};
//void debug(vector<Point> points){ //输出重复的点的情况,并且最后输出去重的vector表
// int len=points.size();
//
// map<int,int>mp;
//
// for(int i=0;i<points.size();i++){
// for(int j=points.size()-1;j>i;j--){
// if(points[i].x==points[j].x&&points[i].y==points[j].y){
// mp[i]++;
// points.erase(points.begin()+j);
// j=points.size();
// }
// }
// }
// map<int,int>::iterator it=mp.begin();
// for(;it!=mp.end();it++){
// cout<<it->first<<"******"<<it->second<<endl;
// }
// cout<<endl;
//}
int main(){ int n;
char arr[12][12];
memset(arr,' ',sizeof(arr));
while(1){ cin>>n;
vector<Point>p;
int x,y; for(int i=0;i<n;i++){
scanf("%d%d",&x,&y); p.push_back(Point(x,y)); // arr[x][y]='*';
} debug(p); Solution s;
cout<<s.maxPoints(p)<<endl; }
// cout<<"_____________________"<<endl;
// for(int i=0;i<10;i++){
// arr[i][11]='\0';
// cout<<arr[i]<<endl;
// }
// cout<<"_____________________"<<endl; return 0;
}

简单测试数据


/*
思路: 穷举,
用例:
9
84 250 0 0 1 0 0 -70 0 -70 1 -1 21 10 42 90 -42 -230
3
1 1 1 1 1 1
4
1 1 1 1 1 1 2 3
5
1 1 1 1 1 1 2 3 2 3
6
1 1 1 1 1 1
2 3 2 3 4 67
1
0 0 用例:
[(84,250),(0,0),(1,0),(0,-70),(0,-70),(1,-1),(21,10),(42,90),(-42,-230)] 对应输出应该为: 6 你的输出为: 96 7 8 9 2 6 4 7 2 9 */
/*
ctrl+E ,复制行
ctrl+shift+A :整体缩进对齐
ctrl+D: 删除当前行
*/

[牛客网 -leetcode在线编程 -01] max-points-on-a-line -穷举的更多相关文章

  1. [牛客网 -leetcode在线编程 -02] minimum-depth-of-binary-tree -树的最短深度

    题目描述 题目描述 Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along ...

  2. 牛客网-Beautiful Land 【01背包 + 思维】

    链接:https://www.nowcoder.com/acm/contest/119/F来源:牛客网 Now HUST got a big land whose capacity is C to p ...

  3. 牛客网/LeetCode/七月在线/HelloWorld114

    除了知乎,还有这些网站与offer/内推/秋招/春招相关. 其中HelloWorld114更是囊括许多IT知识. 当然,我们可以拓宽思考的维度,既然课堂上的老师讲不好,我们可以自己找资源啊= => ...

  4. JavaScript编程那些事(牛客网 LeetCode)

    计算给定数组 arr 中所有元素的总和 本人提供常规方法 function sum(arr) { var len = arr.length; var sum = 0; if(len == 0){ su ...

  5. 牛客网-3 网易编程题(1拓扑&2二叉树的公共最近祖先&3快排找第K大数)

    1. 小明陪小红去看钻石,他们从一堆钻石中随机抽取两颗并比较她们的重量.这些钻石的重量各不相同.在他们们比较了一段时间后,它们看中了两颗钻石g1和g2.现在请你根据之前比较的信息判断这两颗钻石的哪颗更 ...

  6. LeetCode(149) Max Points on a Line

    题目 Given n points on a 2D plane, find the maximum number of points that lie on the same straight lin ...

  7. JS-03 牛客网练习

    1.很多人都使用过牛客网这个在线编程网站,下面是自己做的该项所有练习,已通过网站和老师检查无误,分享给大家. 2.先说一下题目的位置:牛客网https://www.nowcoder.com/activ ...

  8. 2018牛客网暑期ACM多校训练营(第十场)J Rikka with Nickname(二分,字符串)

    链接:https://ac.nowcoder.com/acm/contest/148/J?&headNav=acm 来源:牛客网 Rikka with Nickname 时间限制:C/C++ ...

  9. 牛客网Java刷题知识点之TCP、UDP、TCP和UDP的区别、socket、TCP编程的客户端一般步骤、TCP编程的服务器端一般步骤、UDP编程的客户端一般步骤、UDP编程的服务器端一般步骤

    福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号:   大数据躺过的坑      Java从入门到架构师      人工智能躺过的坑         Java全栈大联盟   ...

随机推荐

  1. ou can mix require and export. You can't mix import and module.exports.

    ou can mix require and export. You can't mix import and module.exports.

  2. 百度地图jsapi 自定义大头针的方法

    百度地图jsapi 自定义大头针的方法<pre> var myIcon = new BMap.Icon("http://developer.baidu.com/map/jsdem ...

  3. spring跨重定向传递数据

    spring跨重定向传递数据 为何要重定向? 作用之一:防止表单重复提交 如何重定向? // 在控制器方法返回的视图名称中,以redirect:开头的String不是用来查找视图的,而是用来指导浏览器 ...

  4. 【LeetCode】三数之和【排序,固定一个数,然后双指针寻找另外两个数】

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...

  5. 【转帖】5G基站建设下的“中国速度”:北上广深领跑全国,均超1万个

    5G基站建设下的“中国速度”:北上广深领跑全国,均超1万个 https://www.laoyaoba.com/html/news/newsdetail?source=pc&news_id=73 ...

  6. vs中web api程序不包含适合于入口点的静态“Main”方法

    步骤:选择该项目的属性--应用程序--输出类型--类库

  7. CodeForces-1152C-Neko does Maths

    C. Neko does Maths time limit per test:1 second memory limit per test:256 megabytes input:standard i ...

  8. python学习-61 类

    类 (在python2里) 1.属性 ---数据属性 ---函数属性 查看属性字典 class chinese: rz:'huangzhong' print(chinese.__dict__) 运行结 ...

  9. windows 开始→运行→命令集锦

    windows 开始→运行→命令集锦 来源于网络,侵权请通知我删除 命令 说明 vwinver 检查Windows版本 wmimgmt.msc 打开windows管理体系结构(WMI) wupdmgr ...

  10. 【scratch3.0教程】1.1 走进编程世界

    第一章 认识Scratch 第1课   走进编程世界 大家认识下图中的人物吗? 史蒂夫·乔布斯             比尔·盖茨 ●Elon Musk,特斯拉.Space X火箭公司创始人,9岁学习 ...