[牛客网 -leetcode在线编程 -01] max-points-on-a-line -穷举
题目及题目来源
链接: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 -穷举的更多相关文章
- [牛客网 -leetcode在线编程 -02] minimum-depth-of-binary-tree -树的最短深度
题目描述 题目描述 Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along ...
- 牛客网-Beautiful Land 【01背包 + 思维】
链接:https://www.nowcoder.com/acm/contest/119/F来源:牛客网 Now HUST got a big land whose capacity is C to p ...
- 牛客网/LeetCode/七月在线/HelloWorld114
除了知乎,还有这些网站与offer/内推/秋招/春招相关. 其中HelloWorld114更是囊括许多IT知识. 当然,我们可以拓宽思考的维度,既然课堂上的老师讲不好,我们可以自己找资源啊= => ...
- JavaScript编程那些事(牛客网 LeetCode)
计算给定数组 arr 中所有元素的总和 本人提供常规方法 function sum(arr) { var len = arr.length; var sum = 0; if(len == 0){ su ...
- 牛客网-3 网易编程题(1拓扑&2二叉树的公共最近祖先&3快排找第K大数)
1. 小明陪小红去看钻石,他们从一堆钻石中随机抽取两颗并比较她们的重量.这些钻石的重量各不相同.在他们们比较了一段时间后,它们看中了两颗钻石g1和g2.现在请你根据之前比较的信息判断这两颗钻石的哪颗更 ...
- 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 ...
- JS-03 牛客网练习
1.很多人都使用过牛客网这个在线编程网站,下面是自己做的该项所有练习,已通过网站和老师检查无误,分享给大家. 2.先说一下题目的位置:牛客网https://www.nowcoder.com/activ ...
- 2018牛客网暑期ACM多校训练营(第十场)J Rikka with Nickname(二分,字符串)
链接:https://ac.nowcoder.com/acm/contest/148/J?&headNav=acm 来源:牛客网 Rikka with Nickname 时间限制:C/C++ ...
- 牛客网Java刷题知识点之TCP、UDP、TCP和UDP的区别、socket、TCP编程的客户端一般步骤、TCP编程的服务器端一般步骤、UDP编程的客户端一般步骤、UDP编程的服务器端一般步骤
福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号: 大数据躺过的坑 Java从入门到架构师 人工智能躺过的坑 Java全栈大联盟 ...
随机推荐
- [LeetCode] 535. Encode and Decode TinyURL 编码和解码短网址
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...
- Windows10不能进入睡眠
问题 有时候暂时不使用计算机(Windows10笔记本),既想省电,又想回来之后快速启动,一般会选择让计算机进入睡眠状态.睡眠状态下,基本上只有内存通电,保存着之前的工作状态,可以快速恢复. 但是最近 ...
- C++ 读取一个文件下所有文件的文件名
Windows: #include<iostream> #include<string> #include <io.h> void readFileNameInDi ...
- C#在linux上运行实现
1 C#开发完了服务 2 部署到linux centos7上 3无法直接运行 解决方法 1 linux cenos7上安装mono 2 执行mono xxx.exe 即可 解决方法2 下载 anye ...
- 【scratch3.0教程】 2.3 奥运五环
(1)编程前的准备 在设计一个作品之前,必须先策划一个脚本,然后再根据脚本,收集或制作素材(图案,声音等),接着就可以启动Scratch,汇入角色.舞台,利用搭程序积木的方式编辑程序,制作出符合脚本的 ...
- Node模块化
Node.js是一个能够在服务器端运行JavaScript的开放源代码.跨平台JavaScript运行环境.Node是对ES标准一个实现,也是一个JS引擎.与传统服务器不同是Node的服务器是单线程的 ...
- Luogu4548 CTSC2006 歌唱王国 概率生成函数、哈希
传送门 orz ymd 考虑构造生成函数:设\(F(x) = \sum\limits_{i=0}^\infty f_ix^i\),其中\(f_i\)表示答案为\(i\)的概率:又设\(G(x) = \ ...
- git学习笔记 ---版本退回
我们已经成功地添加并提交了一个readme.txt文件,现在,是时候继续工作了,于是,我们继续修改readme.txt文件,改成如下内容: Git is a distributed version c ...
- 内网Https 自签Https证书 配合Tomcat 实现内网Https详细图文
内网项目启用Https配置手册 软件需求: OpenSSL https://www.openssl.org/ 已经安装了Java Jdk环境 制作前的需求: 已经配置了Jdk环境变量 安装好OpenS ...
- 全栈项目|小书架|服务器开发-NodeJS 中使用 Sequelize 操作 MySQL数据库
安装 官网:https://sequelize.org/v5/manual/getting-started.html 安装sequelize及数据库连接驱动 npm install --save se ...