📜  计算由斜率在[-K,K]范围内的线连接的坐标对

📅  最后修改于: 2021-04-17 16:44:48             🧑  作者: Mango

给定一个整数K ,两个数组X []Y []都由N个整数组成,其中(X [i],Y [i])是平面中的坐标,任务是查找对的总数点数 从而使通过它们的线的斜率在[-K,K]范围内。

例子:

方法:想法是遍历所有对点,并检查它们的斜率是否在[-K,K]范围内。请按照以下步骤解决问题:

  • 初始化一个变量,将ans表示0,以存储对的结果计数。
  • 现在,生成所有可能的坐标对,如果两个点(X [i],Y [i])(X [j],Y [j])的斜率在[-K,K]范围内,然后将ans1
  • 完成上述步骤后,打印和的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the number of pairs
// of points such that the line passing
// through them has a slope in the range[-k, k]
void findPairs(vector x, vector y,
               int K)
{
    int n = x.size();
 
    // Store the result
    int ans = 0;
 
    // Traverse through all the
    // combination of points
    for (int i = 0; i < n; ++i) {
 
        for (int j = i + 1; j < n; ++j) {
 
            // If pair satisfies
            // the given condition
            if (K * abs(x[i] - x[j])
                >= abs(y[i] - y[j])) {
 
                // Increment ans by 1
                ++ans;
            }
        }
    }
 
    // Print the result
    cout << ans;
}
 
// Driver Code
int main()
{
    vector X = { 2, 1, 0 },
                Y = { 1, 2, 0 };
    int K = 1;
 
    // Function Call
    findPairs(X, Y, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to find the number of pairs
// of points such that the line passing
// through them has a slope in the range[-k, k]
static void findPairs(int[] x, int[] y,
               int K)
{
    int n = x.length;
 
    // Store the result
    int ans = 0;
 
    // Traverse through all the
    // combination of points
    for (int i = 0; i < n; ++i) {
 
        for (int j = i + 1; j < n; ++j) {
 
            // If pair satisfies
            // the given condition
            if (K * Math.abs(x[i] - x[j])
                >= Math.abs(y[i] - y[j])) {
 
                // Increment ans by 1
                ++ans;
            }
        }
    }
 
    // Print the result
    System.out.print(ans);
}
 
 
// Driven Code
public static void main(String[] args)
{
    int[] X = { 2, 1, 0 };
    int[] Y = { 1, 2, 0 };
    int K = 1;
 
    // Function Call
    findPairs(X, Y, K);
}
}
 
// This code is contributed by sanjoy_62.


Python3
# Python3 program for the above approach
 
# Function to find the number of pairs
# of points such that the line passing
# through them has a slope in the range[-k, k]
def findPairs(x, y, K):
    n = len(x)
 
    # Store the result
    ans = 0
 
    # Traverse through all the
    # combination of points
    for i in range(n):
        for j in range(i + 1, n):
           
            # If pair satisfies
            # the given condition
            if (K * abs(x[i] - x[j]) >= abs(y[i] - y[j])):
               
                # Increment ans by 1
                ans += 1
 
    # Print the result
    print (ans)
 
# Driver Code
if __name__ == '__main__':
    X = [2, 1, 0]
    Y = [1, 2, 0]
    K = 1
 
    # Function Call
    findPairs(X, Y, K)
 
 # This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the number of pairs
// of points such that the line passing
// through them has a slope in the range[-k, k]
static void findPairs(int[] x, int[] y,
                      int K)
{
    int n = x.Length;
 
    // Store the result
    int ans = 0;
 
    // Traverse through all the
    // combination of points
    for(int i = 0; i < n; ++i)
    {
        for(int j = i + 1; j < n; ++j)
        {
             
            // If pair satisfies
            // the given condition
            if (K * Math.Abs(x[i] - x[j]) >=
                    Math.Abs(y[i] - y[j]))
            {
                 
                // Increment ans by 1
                ++ans;
            }
        }
    }
 
    // Print the result
    Console.WriteLine(ans);
}
 
// Driver Code
public static void Main(String []args)
{
    int[] X = { 2, 1, 0 };
    int[] Y = { 1, 2, 0 };
    int K = 1;
 
    // Function Call
    findPairs(X, Y, K);
}
}
 
// This code is contributed by souravghosh0416


Javascript


输出:
2

时间复杂度: O(N 2 )
辅助空间: O(1)