给定两个整数M1和M2,代表两条线在一点相交的斜率,任务是找到这两条线之间的角度。
例子:
Input: M1 = 1.75, M2 = 0.27
Output: 45.1455 degrees
Input: M1 = 0.5, M2 = 1.75
Output: 33.6901 degrees
方法:如果θ是两条相交线之间的夹角,那么角度θ可以计算为:
tanθ = |(M2 – M1) / (1 + M1 * M2)|
=> θ = tan-1( |(M2 – M1) / (1 + M1 * M2)| )
请按照以下步骤解决问题:
- 初始化一个变量,比如A ,以存储两条线之间的角度值。
- 使用上面的公式,找到tan(A)的值 并通过取角度的 tan 倒数来更新角度A的值。
- 将角度从弧度转换为度数。
- 以度为单位打印A的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
#define PI 3.14159265
// Function to find the
// angle between two lines
void findAngle(double M1, double M2)
{
// Store the tan value of the angle
double angle = abs((M2 - M1)
/ (1 + M1 * M2));
// Calculate tan inverse of the angle
double ret = atan(angle);
// Convert the angle from
// radian to degree
double val = (ret * 180) / PI;
// Print the result
cout << val;
}
// Driver Code
int main()
{
double M1 = 1.75, M2 = 0.27;
findAngle(M1, M2);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
static double PI = 3.14159265;
// Function to find the
// angle between two lines
static void findAngle(double M1, double M2)
{
// Store the tan value of the angle
double angle = Math.abs((M2 - M1) / (1 + M1 * M2));
// Calculate tan inverse of the angle
double ret = Math.atan(angle);
// Convert the angle from
// radian to degree
double val = (ret * 180) / PI;
// Print the result
System.out.println(val);
}
// Driver Code
public static void main(String []args)
{
double M1 = 1.75, M2 = 0.27;
findAngle(M1, M2);
}
}
// This code is contributed by rrrtnx.
Python3
# Python3 program for the above approach
from math import atan
# Function to find the
# angle between two lines
def findAngle(M1, M2):
PI = 3.14159265
# Store the tan value of the angle
angle = abs((M2 - M1) / (1 + M1 * M2))
# Calculate tan inverse of the angle
ret = atan(angle)
# Convert the angle from
# radian to degree
val = (ret * 180) / PI
# Print the result
print (round(val, 4))
# Driver Code
if __name__ == '__main__':
M1 = 1.75
M2 = 0.27
findAngle(M1, M2)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG
{
static double PI = 3.14159265;
// Function to find the
// angle between two lines
static void findAngle(double M1, double M2)
{
// Store the tan value of the angle
double angle = Math.Abs((M2 - M1) / (1 + M1 * M2));
// Calculate tan inverse of the angle
double ret = Math.Atan(angle);
// Convert the angle from
// radian to degree
double val = (ret * 180) / PI;
// Print the result
Console.Write(val);
}
// Driver Code
public static void Main()
{
double M1 = 1.75, M2 = 0.27;
findAngle(M1, M2);
}
}
// This code is contributed by ukasp.
Javascript
输出:
45.1455
时间复杂度: O(1)
辅助空间: O(1)