📌  相关文章
📜  通过单个点的最大不同线

📅  最后修改于: 2021-10-23 08:16:23             🧑  作者: Mango

给定的N  两点表示的线(x1, y1)  (x2, y2)  .任务是找到可以通过单个点的最大线数,而不叠加(或覆盖)任何其他线。我们可以移动任何线,但不能旋转它。
例子:

Input : Line 1 : x1 = 1, y1 = 1, x2 = 2, y2 = 2
        Line 2 : x2 = 2, y1 = 2, x2 = 4, y2 = 10
Output : 2
There are two lines. These two lines are not 
parallel, so both of them will pass through
a single point.


Input : Line 1 : x1 = 1, y1 = 5, x2 = 1, y2 = 10
        Line 2 : x2 = 5, y1 = 1, x2 = 10, y2 = 1
Output : 2
  • 将线表示为对(m, c)  其中 line 可以表示为y=mx+c  ,称为线斜率形式。我们现在可以看到我们可以更改任何行的c ,但不能修改m
  • 假定(c1 ≠ c2)具有相同m值的线平行。此外,任何两条平行线都不能通过同一点而不相互叠加。
  • 因此,我们的问题简化为从给定的一组线中找到不同的斜率值。

我们可以计算一条线的斜率\frac{(y2-y1)}{(x2-x1)}  ,将它们添加到集合中并计算集合中斜率的不同值的数量。但是我们必须单独处理垂直线。
因此,如果x1 = x2  然后,斜率 = INT_MAX
否则,斜率 = \frac{(y2-y1)}{(x2-x1)}  .
下面是该方法的实现。

C++
// C++ program to find maximum number of lines
// which can pass through a single point
#include 
using namespace std;
 
// function to find maximum lines which passes
// through a single point
int maxLines(int n, int x1[], int y1[],
                int x2[], int y2[])
{
    unordered_set s;
 
    double slope;
    for (int i = 0; i < n; ++i) {
        if (x1[i] == x2[i])
            slope = INT_MAX;
        else
            slope = (y2[i] - y1[i]) * 1.0
                    / (x2[i] - x1[i]) * 1.0;
 
        s.insert(slope);
    }
 
    return s.size();
}
 
// Driver program
int main()
{
    int n = 2, x1[] = { 1, 2 }, y1[] = { 1, 2 },
            x2[] = { 2, 4 }, y2[] = { 2, 10 };
    cout << maxLines(n, x1, y1, x2, y2);
    return 0;
}
// This code is written by
// Sanjit_Prasad


Java
// Java program to find maximum number of lines
// which can pass through a single point
 
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG{
 
// function to find maximum lines which passes
// through a single point
static int maxLines(int n, int x1[], int y1[],
                    int x2[], int y2[])
{
    Set s=new HashSet();
 
    double slope;
    for (int i = 0; i < n; ++i) {
        if (x1[i] == x2[i])
            slope = Integer.MAX_VALUE;
        else
            slope = (y2[i] - y1[i]) * 1.0
                    / (x2[i] - x1[i]) * 1.0;
 
        s.add(slope);
    }
 
    return s.size();
}
 
// Driver program
public static void main(String args[])
{
    int n = 2, x1[] = { 1, 2 }, y1[] = { 1, 2 },
            x2[] = { 2, 4 }, y2[] = { 2, 10 };
    System.out.print(maxLines(n, x1, y1, x2, y2));
}
}
// This code is written by
// Subhadeep


Python3
# Python3 program to find maximum number
# of lines which can pass through a
# single point
import sys
# function to find maximum lines
# which passes through a single point
def maxLines(n, x1, y1, x2, y2):
 
    s = [];
 
    slope=sys.maxsize;
    for i in range(n):
        if (x1[i] == x2[i]):
            slope = sys.maxsize;
        else:
            slope = (y2[i] - y1[i]) * 1.0 /(x2[i] - x1[i]) * 1.0;
 
        s.append(slope);
 
    return len(s);
 
# Driver Code
n = 2;
x1 = [ 1, 2 ];
y1 = [1, 2];
x2 = [2, 4];
y2 = [2, 10];
print(maxLines(n, x1, y1, x2, y2));
 
# This code is contributed by mits


C#
// C# program to find maximum number of lines
// which can pass through a single point
using System;
using System.Collections.Generic;
 
class GFG
{
 
// function to find maximum lines which passes
// through a single point
static int maxLines(int n, int []x1, int []y1,
                    int []x2, int []y2)
{
    HashSet s = new HashSet();
 
    double slope;
    for (int i = 0; i < n; ++i)
    {
        if (x1[i] == x2[i])
            slope = int.MaxValue;
        else
            slope = (y2[i] - y1[i]) * 1.0
                    / (x2[i] - x1[i]) * 1.0;
 
        s.Add(slope);
    }
 
    return s.Count;
}
 
// Driver code
public static void Main()
{
    int n = 2;
    int []x1 = { 1, 2 }; int []y1 = { 1, 2 };
    int []x2 = { 2, 4 }; int []y2 = { 2, 10 };
    Console.Write(maxLines(n, x1, y1, x2, y2));
}
}
 
/* This code contributed by PrinciRaj1992 */


PHP


Javascript


输出:

2

时间复杂度: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程