给定两条分别具有系数a1x + b1y + c1 = 0和a2x + b2y + c2 = 0的直线,任务是检查这些直线是否相同。
例子:
Input: a1 = -2, b1 = 4, c1 = 3, a2 = -6, b2 = 12, c2 = 9
Output: The given straight lines are identical
Input: a1 = 12, b1 = 3, c1 = 8, a2 = 7, b2 = -12, c2 = 0
Output: The given straight lines are not identical
方法:
- 给定方程式
a1x + b1y + c1 = 0
a2x + b2y + c2 = 0 - 将它们转换为斜率截距形式,我们得到
y =(-a1 / b1)x +(-c1 / b1)
y =(-a2 / b2)x +(-c2 / b2) - 现在,如果线是相同的,那么斜率和截距必须相等,
所以,
-a1 / b1 = -a2 / b2
或者,a1 / a2 = b1 / b2
还,
-c1 / b1 = -c2 / b2
因此,c1 / c2 = b1 / b2 - 因此,如果两条给定的直线相同,则系数应该成比例。
下面是上述方法的实现
C++
// C++ program to check if
// given two straight lines
// are identical or not
#include
using namespace std;
// Function to check if they are identical
void idstrt(double a1, double b1,
double c1, double a2,
double b2, double c2)
{
if ((a1 / a2 == b1 / b2)
&& (a1 / a2 == c1 / c2)
&& (b1 / b2 == c1 / c2))
cout << "The given straight"
<< " lines are identical"
<< endl;
else
cout << "The given straight"
<< " lines are not identical"
<< endl;
}
// Driver Code
int main()
{
double a1 = -2, b1 = 4,
c1 = 3, a2 = -6,
b2 = 12, c2 = 9;
idstrt(a1, b1, c1, a2, b2, c2);
return 0;
}
Java
// Java program to check if
// given two straight lines
// are identical or not
class GFG
{
// Function to check if they are identical
static void idstrt(double a1, double b1,
double c1, double a2,
double b2, double c2)
{
if ((a1 / a2 == b1 / b2)
&& (a1 / a2 == c1 / c2)
&& (b1 / b2 == c1 / c2))
System.out.println( "The given straight"
+" lines are identical");
else
System.out.println("The given straight"
+ " lines are not identical");
}
// Driver Code
public static void main(String[] args)
{
double a1 = -2, b1 = 4,
c1 = 3, a2 = -6,
b2 = 12, c2 = 9;
idstrt(a1, b1, c1, a2, b2, c2);
}
}
// This code has been contributed by 29AjayKumar
Python3
# Python3 program to check if
# given two straight lines
# are identical or not
# Function to check if they are identical
def idstrt(a1, b1, c1, a2, b2, c2):
if ((a1 // a2 == b1 // b2) and
(a1 // a2 == c1 // c2) and
(b1 // b2 == c1 // c2)):
print("The given straight lines",
"are identical");
else:
print("The given straight lines",
"are not identical");
# Driver Code
a1, b1 = -2, 4
c1, a2 = 3,-6
b2, c2 = 12,9
idstrt(a1, b1, c1, a2, b2, c2)
# This code is contributed
# by mohit kumar
C#
// C# program to check if
// given two straight lines
// are identical or not
using System;
class GFG
{
// Function to check if they are identical
static void idstrt(double a1, double b1,
double c1, double a2,
double b2, double c2)
{
if ((a1 / a2 == b1 / b2)
&& (a1 / a2 == c1 / c2)
&& (b1 / b2 == c1 / c2))
Console.WriteLine( "The given straight"
+" lines are identical");
else
Console.WriteLine("The given straight"
+ " lines are not identical");
}
// Driver Code
public static void Main(String[] args)
{
double a1 = -2, b1 = 4,
c1 = 3, a2 = -6,
b2 = 12, c2 = 9;
idstrt(a1, b1, c1, a2, b2, c2);
}
}
// This code contributed by Rajput-Ji
PHP
Javascript
输出:
The given straight lines are identical