九度OJ 1548 map的用法

题目来源: http://ac.jobdu.com/problem.php?pid=1548
题目描述:

给定平面上的n个点,任意做一条直线,求至多能有几个点恰好落在直线上。

输入:

包含多组测试数据,每组测试数据由一个整数n(0<=n<=100)开始,代表平面上点的个数。
接下去n行每行给出一个点的坐标(x,y),x、y的绝对值均小于等于100。

输出:

对于每组测试数据,输出一个整数,表示至多能有几个点恰好落在直线上。

样例输入:
2
0 0
1 1
4
0 0
1 1
2 2 
3 6
样例输出:
2
3

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string>
 4 #include<string.h>
 5 #include<map>
 6 #include<math.h>
 7 #define N 105
 8 using namespace std;
 9 double Point[N][2];
10 int n;
11 //定义分数结构体
12 struct Node
13 {
14     double zi;
15     double mu;
16     Node(){};
17     Node(double z,double m):zi(z),mu(m){};                      // 函数构造
18     bool operator< (const Node &B)const                        //  结构体小于符号重载
19     {
20         return zi*B.mu < mu*B.zi;
21     }
22 
23     bool operator== ( const Node &B)const                     //结构体等于符号重载
24     {
25         return zi*B.mu == mu*B.zi;
26     }
27 };
28 
29 int main()
30 {
31     while(cin>>n)
32     {
33         int i;
34         for(i=0;i<n;i++)
35         {
36             cin>>Point[i][0]>>Point[i][1];
37         }
38         int ans=0;
39         for(i=0;i<n;i++)
40         {
41                 double x= Point[i][0];
42                 double y= Point[i][1];
43                 int j;
44                 map<Node,int>mp;
45                 map<Node,int>::iterator it;
46                 mp.clear();               //mp需要清空
47                 int an=0;          
48                 for(j=0;j<n;j++)
49                 {
50                     if(i==j) continue;
51                     if(x ==Point[j][0])
52                         an++;
53                     else
54                         mp[Node(Point[j][1]-y,Point[j][0] - x)]++;
55                 }
56                 for(it=mp.begin();it!=mp.end();it++)
57                         an=max(an,it->second);                      // an记录当前点i, 与其他n-1 个点 的所有斜率中 ,斜率相等的最大数目, 则最大的顶点数位 an+1
58 
59             ans=max(ans,an+1);
60         }
61         cout<<ans<<endl;
62     }
63     return 0 ;
64 }



原文地址:https://www.cnblogs.com/zn505119020/p/3575104.html