Dados dos números enteros A y B que representan el tiempo que tardan dos temporizadores de arena diferentes en vaciarse. La tarea es encontrar el conteo de vueltas de cada temporizador hasta la instancia en la que ambos temporizadores de arena se vacían al mismo tiempo.
Ejemplos:
Entrada: A = 30, B = 15
Salida: 0 1
Después de 15 minutos: 15 0
Flip timer 2: 15 15
Después de otros 15 minutos: 0 0
Entrada: A = 10, B = 8
Salida: 3 4
Enfoque: el factor común más bajo (LCM) de dos números determinará el momento en que ambos temporizadores de arena se vaciarán juntos.
LCM(A, B) = (A * B) / GCD(A, B)
Dividir el LCM por la entrada dará el número de vueltas para cada reloj de arena respectivamente.
C++
//C++14 implementation of the approach #include<bits/stdc++.h> using namespace std; //Recursive function to return //the gcd of a and b int gcd(int a, int b){ //Everything divides 0 if (b == 0) return a; return gcd(b, a % b); } //Function to print the number of //flips for both the sand timers void flip(int a,int b){ int lcm =(a * b)/gcd(a, b); a = lcm/a; b = lcm/b; cout<<a-1<<" "<<b-1; } //Driver code int main(){ int a = 10; int b = 5; flip(a, b); }
Java
// Java implementation of the approach class GFG { // Recursive function to return // the gcd of a and b static int gcd(int a, int b) { // Everything divides 0 if (b == 0) return a; return gcd(b, a % b); } // Function to print the number of // flips for both the sand timers static void flip(int a, int b) { int lcm = (a * b) / gcd(a, b); a = lcm / a; b = lcm / b; System.out.print((a - 1) + " " + (b - 1)); } // Driver code public static void main(String[] args) { int a = 10; int b = 5; flip(a, b); } } // This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach # Recursive function to return # the gcd of a and b def gcd(a, b): # Everything divides 0 if (b == 0): return a return gcd(b, a % b) # Function to print the number of # flips for both the sand timers def flip(a, b): lcm = (a * b) // gcd(a, b) a = lcm // a b = lcm // b print(a - 1, b - 1) # Driver code a = 10 b = 5 flip(a, b) # This code is contributed by Mohit Kumar
C#
// C# implementation of the approach using System; class GFG { // Recursive function to return // the gcd of a and b static int gcd(int a, int b) { // Everything divides 0 if (b == 0) return a; return gcd(b, a % b); } // Function to print the number of // flips for both the sand timers static void flip(int a, int b) { int lcm = (a * b) / gcd(a, b); a = lcm / a; b = lcm / b ; Console.WriteLine((a - 1) + " " + (b - 1)); } // Driver code public static void Main() { int a = 10; int b = 5; flip(a, b); } } // This code is contributed by AnkitRai01
Javascript
<script> // Javascript implementation of the approach // Recursive function to return // the gcd of a and b function gcd(a, b) { // Everything divides 0 if (b == 0) return a; return gcd(b, a % b); } // Function to print the number of // flips for both the sand timers function flip(a, b){ let lcm =parseInt((a * b)/gcd(a, b)); a = parseInt(lcm/a); b = parseInt(lcm/b); document.write((a-1)+" "+(b-1)); } // Driver code let a = 10; let b = 5; flip(a, b); // This code is contributed by subham348. </script>
0 1
Publicación traducida automáticamente
Artículo escrito por divyamohan123 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA