给定两条直线(a1,b1,c1)和(a2,b2,c2)的等式,使得(ai,bi,ci)分别是X 2的系数, X和直线的常数项方程 ,任务是检查两条直线是否平行。如果发现它们是平行的,则打印“是” 。否则,打印“否” 。
例子:
Input: a1 = -2, b1 = 4, a2 = -6, b2 = 12
Output: Yes
Explanation:
The slope of both lines are equal i.e., a1/b1 = a2/ b2 = -2.
Input: a1 = 11, b1 = 3, a2 = 7, b2 = -10
Output: No
Explanation:
The slope of both lines are not equal i.e., a1/b1≠ a2/b2.
方法:要检查两条线是否彼此平行,其思想是比较给定线的斜率。如果给定线的斜率相等,则给定线是平行的。因此,打印“是”,否则打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if two lines
// are parallel or not
void parallel(float a1, float b1,
float c1, float a2,
float b2, float c2)
{
// If slopes are equal
if ((-(a1 / b1)) == (-(a2 / b2))) {
cout << "Yes";
}
else {
cout << "No";
}
}
// Driver Code
int main()
{
float a1 = -2, b1 = 4, c1 = 5;
float a2 = -6, b2 = 12, c2 = 6;
// Function Call
parallel(a1, b1, c1, a2, b2, c2);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
// Function to check if two lines
// are parallel or not
static void parallel(float a1, float b1,
float c1, float a2,
float b2, float c2)
{
// If slopes are equal
if ((-(a1 / b1)) == (-(a2 / b2)))
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
}
// Driver Code
public static void main(String args[])
{
float a1 = -2, b1 = 4, c1 = 5;
float a2 = -6, b2 = 12, c2 = 6;
// Function Call
parallel(a1, b1, c1, a2, b2, c2);
}
}
// This code is contributed by splevel62.
Python3
# Python program to implement
# the above approach
# Function to check if two lines
# are parallel or not
def parallel(a1, b1, c1, a2, b2, c2):
# If slopes are equal
if ((-(a1 / b1)) == (-(a2 / b2))):
print("Yes");
else:
print("No");
# Driver Code
if __name__ == '__main__':
a1 = -2; b1 = 4; c1 = 5;
a2 = -6; b2 = 12; c2 = 6;
# Function Call
parallel(a1, b1, c1, a2, b2, c2);
# This code is contributed by 29AjayKumar
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to check if two lines
// are parallel or not
static void parallel(float a1, float b1,
float c1, float a2,
float b2, float c2)
{
// If slopes are equal
if ((-(a1 / b1)) == (-(a2 / b2)))
{
Console.Write("Yes");
}
else
{
Console.Write("No");
}
}
// Driver Code
public static void Main()
{
float a1 = -2, b1 = 4, c1 = 5;
float a2 = -6, b2 = 12, c2 = 6;
// Function Call
parallel(a1, b1, c1, a2, b2, c2);
}
}
// This code is contributed by susmitakundugoaldanga.
输出:
Yes
时间复杂度: O(1)
辅助空间: O(1)