#C9176. Maximum Towns on a Straight Line
Maximum Towns on a Straight Line
Maximum Towns on a Straight Line
Given a set of points representing the coordinates of towns in a 2D plane, your task is to determine the maximum number of towns that lie on a single straight line.
For any two distinct points \( (x_1,y_1) \) and \( (x_2,y_2) \), they lie on the same line if the slope between them is constant. The slope is computed as:
\( \text{slope} = \frac{y_2 - y_1}{x_2 - x_1} \)
Note that when \( x_1 = x_2 \), the line is vertical, and should be handled as a special case. Also, if duplicate points are present, they should be counted multiple times.
The input consists of several datasets. For each dataset, the first line gives an integer \( n \) indicating the number of towns (points). The next \( n \) lines each contain two integers representing the \( x \) and \( y \) coordinates of a town. The datasets are terminated by a line containing 0. For each dataset, output the maximum number of towns that can be found on one straight line.
inputFormat
The input is read from stdin and consists of multiple datasets. For each dataset:
- The first line contains an integer \( n \) (\( n \geq 0 \)); if \( n = 0 \), it marks the end of input.
- The following \( n \) lines each contain two space-separated integers representing the \( x \) and \( y \) coordinates of a town.
For example:
5 1 1 2 2 3 3 4 1 5 1 4 0 0 1 1 2 2 3 4 0
outputFormat
For each dataset, output a single line to stdout with an integer indicating the maximum number of towns that lie along the same straight line.
For the sample input above, the output should be:
3 3## sample
5
1 1
2 2
3 3
4 1
5 1
4
0 0
1 1
2 2
3 4
0
3
3
</p>