给定三个整数M , H和V分别表示物体的质量,速度和高度,任务是计算其动能以及势能,
注意:由于重力(g)引起的加速度值为9.8,并且忽略单位。
例子:
Input: M = 25, H = 10, V = 15
Output:
Kinetic Energy = 2812.5
Potential Energy = 2450
Explanation : The kinetic energy of the particle is 2812.5 and the potential energy is 2450.
Input : M=5.5, H=23.5, V= 10.5
Output :
303.188
1266.65
方法:可以使用以下两个公式来计算所需的动能和势能值:
Kinetic Energy = 0.5 * Mass ( M ) * Velocity ( V ) 2
Potential Energy = Mass ( M ) * Height ( H ) * Acceleration due to gravity ( g )
下面是上述方法的实现:
C++14
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to calculate Kinetic Energy
float kineticEnergy(float M, float V)
{
// Stores the Kinetic Energy
float KineticEnergy;
KineticEnergy = 0.5 * M * V * V;
return KineticEnergy;
}
// Function to calculate Potential Energy
float potentialEnergy(float M, float H)
{
// Stores the Potential Energy
float PotentialEnergy;
PotentialEnergy = M * 9.8 * H;
return PotentialEnergy;
}
// Driver Code
int main()
{
float M = 5.5, H = 23.5, V = 10.5;
cout << "Kinetic Energy = "
<< kineticEnergy(M, V) << endl;
cout << "Potential Energy = "
<< potentialEnergy(M, H) << endl;
return 0;
}
Python3
# Python3 program to implement
# the above approach
# Function to calculate Kinetic Energy
def kineticEnergy(M, V):
# Stores the Kinetic Energy
KineticEnergy = 0.5 * M * V * V
return KineticEnergy
# Function to calculate Potential Energy
def potentialEnergy(M, H):
# Stores the Potential Energy
PotentialEnergy = M * 9.8 * H
return PotentialEnergy
# Driver Code
if __name__ == "__main__":
M = 5.5
H = 23.5
V = 10.5
print("Kinetic Energy = ", kineticEnergy(M, V))
print("Potential Energy = ", potentialEnergy(M, H))
# This code is contributed by AnkThon
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
/// Function to calculate Kinetic Energy
static double kineticEnergy(double M, double V)
{
// Stores the Kinetic Energy
double KineticEnergy;
KineticEnergy = 0.5 * M * V * V;
return KineticEnergy;
}
// Function to calculate Potential Energy
static double potentialEnergy(double M, double H)
{
// Stores the Potential Energy
double PotentialEnergy;
PotentialEnergy = M * 9.8 * H;
return PotentialEnergy;
}
// Driver Code
public static void Main()
{
double M = 5.5, H = 23.5, V = 10.5;
Console.WriteLine("Kinetic Energy = " +
kineticEnergy(M, V));
Console.Write("Potential Energy = " +
potentialEnergy(M, H));
}
}
// This code is contributed by bgangwar59
输出:
Kinetic Energy = 303.188
Potential Energy = 1266.65
时间复杂度: O(1)
辅助空间: O(1)