# Zzuli? OJ? 1196: counting stars (II) (special subject of structure)

Knowledge point: using structure array parameter, sort function

#### Title Description

One day, Xiaoming sat in the yard and counted the stars. Gardon asked him to count the number of stars in the sky on the same straight line. There are too many stars in the sky. Xiao Ming will be blinded immediately. Can you write a program to help him calculate?

#### input

First, enter an integer N (N < = 300), and the next N logarithm represents the position of a star (the coordinates of the star are between - 10000 and 10000, accurate to 1 decimal place). No two stars will be in the same place.

#### output

An integer representing the maximum number of stars in a line.

#### sample input

5

0 0

1 0

1 1

0 1

0.5 0.5

#### sample output

3

#### Train of thought:

Two points can determine a straight line, so N points have N × (N-1) straight lines, so how to judge those lines are collinear?

A collinear line has the characteristic of having the same point and the same slope. So we can design the algorithm as follows:

(1) If the point is less than 3, the direct output point is the result;

(2) Take the first as the common ground;

(3) Calculate the slope of other points and their lines (slope calculation method: k=(x1-x0)/(x2-x0)=(y1-y0)(y2-y0));

(4) In order to rank the slopes, the largest number with the same slope is counted;

(5) The following is a common point, turn (3);

(6) Output Max.

ps: that is, use for loop nesting, p0, p1, p2 three points to constantly calculate the slope to see whether it is equal, and because there is something in common, so to judge whether the slope is the same is to judge whether it is on the same line.

Error reason: since the number of elements of b [] is ignored, the maximum number can be n * (n-1), so the array is small, and I have been reminded that the pointer is out of bounds.

#### Method 1: use only structure array:

```
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
typedef struct point
{
float x;
float y;
} P;
double multi(point p0, point p1,point p2);
int main()
{
P p[301];
int n;
while(cin>>n)
{
for(int i = 0; i < n; i++)
{
cin>>p[i].x>>p[i].y;
}
int m = 0;
int b[100005];
memset(b,0,sizeof(b));
for(int i = 0; i < n-1; i++)
for(int j = i + 1; j < n; j++)
{
for(int k = 0; k < n; k++)
if(multi(p[i], p[j], p[k]) == 0)
b[m]++;
m++;
}
sort(b, b+m);
cout<<b[m-1]<<endl;
}
return 0;
}
double multi(point p0, point p1,point p2)
{
return (p1.x - p0.x)*(p2.y - p0.y)-(p2.x - p0.x)*(p1.y - p0.y);
}
```

#### Method 2: at the same time, use the function to pass parameters:

```
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
typedef struct point
{
float x;
float y;
} P; //Coordinate coordinate coordinate system
void Creat(P p[], int n);
void Judge(P p[], int n);
double multi(point p0, point p1,point p2);
int main()
{
P p[301];
int n;
while(cin>>n)
{
Creat(p, n);
Judge(p, n);
}
return 0;
}
void Creat(P p[], int n)
{
for(int i = 0; i < n; i++)
{
cin>>p[i].x>>p[i].y;
}
}
void Judge(P p[], int n)
{
int m = 0;
int b[100005];
memset(b,0,sizeof(b));
for(int i = 0; i < n-1; i++)
for(int j = i + 1; j < n; j++)
{
for(int k = 0; k < n; k++)
if(multi(p[i], p[j], p[k]) == 0)
b[m]++;
m++;
}
sort(b, b+m);
cout<<b[m-1]<<endl;
}
double multi(point p0, point p1,point p2)
{
return (p1.x - p0.x)*(p2.y - p0.y)-(p2.x - p0.x)*(p1.y - p0.y);
}
```