给定A在100米比赛中给予B和C的领先优势。任务是找到在同一个种族中B可以给予C的领先优势。
例子:
Input: B = 10 meters, C = 28 meters
Output: 20 meters
B can give C a start of 20 meters.
Input: B = 20 meters, C = 50 meters
Output: 62 meters
B can give C a start of 62 meters.
方法:
Total meters in a race = 100 meters.
A is ahead of B by 10 meters. When A completed it’s 100 meters B completed it’s 90 meters.
Similarly, A is ahead of C by 28 meters. When A completed it’s 100 meters C completed it’s 72 meters.
Now, When B completed it’s 90 meters C completed it’s 72 meters.
So when B completed it’s 100 meters C completed it’s 80 meters.
–> (( C * 100) / B)
–> (( 72 * 100) / 90) i.e 80 meters
So B can give C a start of 20 meters
下面是上述方法的实现:
C++
// C++ implementation of above approach
#include
using namespace std;
// Function to find the B start to C
int Race(int B, int C)
{
int result = 0;
// When B completed it's 100 meter
// then Completed meters by C is
result = ((C * 100) / B);
return 100 - result;
}
// Driver Code.
int main()
{
int B = 10, C = 28;
// When A completed it's 100 meter
// Then completed meters of B and C is
B = 100 - B;
C = 100 - C;
cout << Race(B, C) << " meters";
return 0;
}
Java
// Java implementation of above approach
public class GFG
{
// Function to find the B start to C
static int Race(int B, int C)
{
int result = 0;
// When B completed it's 100 meter
// then Completed meters by C is
result = ((C * 100) / B);
return 100 - result;
}
// Driver Code
public static void main(String[] args)
{
int B = 10;
int C = 28;
// When A completed it's 100 meter
// Then completed meters of B and C is
B = 100 - B;
C = 100 - C;
System.out.println(Race(B, C) + " meters");
}
}
// This code is contributed
// by ChitraNayal
Python3
# Python 3 implementation
# of above approach
# Function to find the
# B start to C
def Race(B, C):
result = 0;
# When B completed it's 100 meter
# then Completed meters by C is
result = ((C * 100) // B)
return 100 - result
# Driver Code
if __name__ == "__main__":
B = 10
C = 28
# When A completed it's 100 meter
# Then completed meters of B and C is
B = 100 - B;
C = 100 - C;
print(str(Race(B, C)) + " meters")
# This code is contributed
# by ChitraNayal
C#
// C# implementation of above approach
using System;
class GFG
{
// Function to find the B start to C
static int Race(int B, int C)
{
int result = 0;
// When B completed it's 100 meter
// then Completed meters by C is
result = ((C * 100) / B);
return 100 - result;
}
// Driver Code
public static void Main()
{
int B = 10;
int C = 28;
// When A completed it's 100 meter
// Then completed meters of B and C is
B = 100 - B;
C = 100 - C;
Console.Write(Race(B, C) + " meters");
}
}
// This code is contributed
// by ChitraNayal
PHP
Javascript
输出:
20 meters