给定一个整数m ,它是一条直线的斜率,任务是找到与给定直线平行的直线的斜率。
例子:
Input: m = 2
Output: 2
Input: m = -3
Output: -3
方法:
设P和Q是两条平行线,方程分别为y = m1x + b1和y = m2x + b2 。这里m1和m2分别是直线的斜率。现在由于线是平行的,它们没有任何交点,因此将没有线的解决方案系统。所以,让我们试着解方程,
For y, m1x + b1 = m2x + b2
m1x – m2x = b2 – b1
x(m1 – m2) = b2 – b1
The only way there can be no solution for x is for m1 – m2 to be equal to zero.
m1 – m2 = 0
This gives us m1 = m2 and the slopes are equal.
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the slope
// of the line which is parallel to
// the line with the given slope
double getSlope(double m)
{
return m;
}
// Driver code
int main()
{
double m = 2;
cout << getSlope(m);
return 0;
}
Java
// Java implementation of the approach
class GfG
{
// Function to return the slope
// of the line which is parallel to
// the line with the given slope
static double getSlope(double m)
{
return m;
}
// Driver code
public static void main(String[] args)
{
double m = 2;
System.out.println(getSlope(m));
}
}
// This code is contributed by Code_Mech.
Python3
# Python3 implementation of the approach
# Function to return the slope
# of the line which is parallel to
# the line with the given slope
def getSlope(m):
return m;
# Driver code
m = 2;
print(getSlope(m));
# This code is contributed
# by Akanksha Rai
C#
// C# implementation of the approach
class GFG
{
// Function to return the slope
// of the line which is parallel to
// the line with the given slope
static double getSlope(double m)
{
return m;
}
// Driver code
static void Main()
{
double m = 2;
System.Console.Write(getSlope(m));
}
}
// This code is contributed by mits
PHP
Javascript
输出:
2