码迷,mamicode.com
首页 > 其他好文 > 详细

关于多点共线问题

时间:2019-03-19 21:27:50      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:push   cond   for   const   public   ret   turn   lin   vector   

想了想这个问题,自己写了写,之前百度了个答案,思想基本类似

#include <vector>
#include <map>

 class point
 {
 public:
     point(int x0,int x1):x(x0),y(x1){};
     ~point(){};
     int getX() const {return x;}
     int getY() const {return y;}
 private:
     int x;
     int y;
 };
 int getMaxOnline(vector<point>& input)
 {
     map<int,int> map1;
     static int key = 0;
     int max = 0;
     for(int i =0;i<input.size();++i)
     {
         for(int k =0;k<input.size();++k)
         {
             if(i != k)
             {
                 key ++;
                 int x0 = input[i].getX() - input[k].getX();
                 int y0 = input[i].getY() - input[k].getY();
                  for(int j =0;j<input.size();++j)
                  {
                     if(j != k && j != i )
                     {
                         int x1 =  input[j].getX() - input[k].getX();
                         int y1 =  input[j].getY() - input[k].getY();
                         if(x0*y1 - y0*x1 == 0)
                         {
                             map1[key]++;
                         }
                     }
                  }
             }
         }
     }
     map<int,int>::iterator it;
     for(it = map1.begin();it != map1.end();++it)
     {
         if(it->second >= max)
         {
             max = it->second;
         }
     }
     return max+2;
 }

 void main()

 {
    int x,y;
    vector<point> vec;
    for(int i = 0;i < 5 ;++i)
    {
        cin >>x>>y;
        point a(x,y);
        vec.push_back(a);
    }
    int max = getMaxOnline(vec);
    cout<<"max online is "<<max <<endl;
    system("pause");
 }

技术图片

 

关于多点共线问题

标签:push   cond   for   const   public   ret   turn   lin   vector   

原文地址:https://www.cnblogs.com/doulcl/p/10561324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!