Dada la ventaja que A da a B y C en una carrera de 100 metros. La tarea es encontrar la ventaja inicial que B puede darle a C en la misma carrera.
Ejemplos:
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.
Acercarse:
Total de metros en una carrera = 100 metros.
A está delante de B por 10 metros. Cuando A completó sus 100 metros B completó sus 90 metros.
De manera similar, A está por delante de C por 28 metros. Cuando A completó sus 100 metros C completó sus 72 metros.
Ahora, cuando B completó sus 90 metros, C completó sus 72 metros.
Entonces, cuando B completó sus 100 metros, C completó sus 80 metros.
–> (( C * 100) / B)
–> (( 72 * 100) / 90) es decir, 80 metros
Entonces B puede darle a C un comienzo de 20 metros
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of above approach #include <bits/stdc++.h> 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
<?php // PHP implementation of above approach // Function to find the B start to C function 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 $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; echo Race($B, $C) . " meters"; // This code is contributed // by ChitraNayal ?>
Javascript
<script> // Javascript implementation of above approach // Function to find the B start to C function Race(B, C) { var result = 0; // When B completed it's 100 meter // then Completed meters by C is result = ((C * 100) / B); return 100 - result; } // Driver Code var 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; document.write(Race(B, C) + " meters"); // This code is contributed by itsok </script>
20 meters
Publicación traducida automáticamente
Artículo escrito por Naman_Garg y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA